require.extensions处理程序:
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
否则,您可以
fs.readFile与
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);});
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)