我想知道如何将npm模块导出到另一个文件,而不使用该文件上所需的函数。
-index.js-
const cv = require("opencv4nodejs");
const add = require("./addon");
cv.imshowWait("", add.img());我的主要javascript文件显示如下
-addon.js-
const cv = require("opencv4nodejs");
exports.img = () =>{
return cv.imread("lenna.jpg")
}我的导出javascript文件如下所示
简单地说,在hear addon.js文件中,读取图像文件并导出到index.js文件中,然后在hear文件中显示图像。它运转得很好
但是我想删除require函数。
const cv = require("opencv4nodejs");在addon.js文件上
并将该npm模块从index.js文件导出到addon.js文件。类似于index.js文件中的内容
const cv = require("opencv4nodejs");
const add = require("./addon")(cv);怎么做那些家伙..。
发布于 2020-11-17 11:57:03
您需要导出一个接受cv并使其在模块中可用的函数。所以,就像这样:
// addon.js
var cv;
const img = () =>{
return cv.imread("lenna.jpg")
}
module.exports = function (opencv4nodejs) {
cv = opencv4nodejs;
return { img };
}然后,正如您已经展示的那样,使用它:
// index.js
const cv = require("opencv4nodejs");
const add = require("./addon")(cv);
cv.imshowWait("", add.img());https://stackoverflow.com/questions/64874486
复制相似问题