我想出了如何在 Node JS 中包含一个 JS 文件。但是我有一个 JS 文件 (hashing.js),它使用了来自其他 JS 文件 (encoding.js) 的函数。
在 encoding.js 中,我有
exports = exports || {};
exports.encoding = encoding function
在 hashing.js 中,我有
exports = exports || {};
exports.hashing = hashing function
散列函数在其内部使用编码。
我将它们包含在 Node JS 中
var encoding = require (./encoding.js);
var hashing = require (./hashing.js);
但是当我像这样包含 JS 文件时,运行散列变量会抛出错误
encoding is not defined
所以我无法在依赖于其他一些 JS 文件的 Node JS 中包含 JS 文件。
最佳答案
不要那样做
exports = exports || {};
exports.encoding = encoding function
那样做
module.exports = function(){}
或
exports.encoding = function(){}
然后
var encoding = require (./encoding).encoding;
我建议你花点时间阅读:
关于javascript - 在 Node JS 中包含依赖的 JS 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23908372/