If it's for a (few) specific extension(s), you can add your own require.extensions
handler:
var fs = require('fs');
require.extensions['.txt'] = function (module, filename) {
module.exports = fs.readFileSync(filename, 'utf8');
};
var words = require("./words.txt");
console.log(typeof words); // string
Otherwise, you can mix fs.readFile
with require.resolve
:
var fs = require('fs');
function readModuleFile(path, callback) {
try {
var filename = require.resolve(path);
fs.readFile(filename, 'utf8', callback);
} catch (e) {
callback(e);
}
}
readModuleFile('./words.txt', function (err, words) {
console.log(words);
});
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…