nodejs中自定义模块中的一个小坑,记录一下;
在nodejs中,一般一个js文件作为一个模块,最后以 module.exports = xxx; 结尾;就可以在其他文件引用了,我这里有自定义模块代码如下
const puppeteer = require('puppeteer');
class Craw {
constructor() {
}
async init() {
this.browser = await puppeteer.launch({
headless: false // 关闭无头模式
});
this.page = (await this.browser.pages())[0]
}
}
module.exports = Craw;
这是一个puppeteer,准备做一个请求主动探测,作为一个漏扫工具的一部分;然后想在其他模块中调用,调用模块代码如下
const Craw = require('./libs/craw')
(async () => {
const craw1 = new Craw();
await craw1.init();
})();
但是问题来了,这时候竟然报错了一脸的黑人问号!!!!
报错如下:
"D:\Program Files\nodejs\node.exe" F:\GoProject\swordscan\docker\pupcraw\main.js F:\GoProject\swordscan\docker\pupcraw\main.js:3 (async () => { ^ TypeError: Class constructor Craw cannot be invoked without 'new' at Object.<anonymous> (F:\GoProject\swordscan\docker\pupcraw\main.js:3:1) at Module._compile (internal/modules/cjs/loader.js:1256:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1277:10) at Module.load (internal/modules/cjs/loader.js:1105:32) at Function.Module._load (internal/modules/cjs/loader.js:967:14) at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12) at internal/main/run_main_module.js:17:47 Process finished with exit code 1
各种问题各种找;最后发现,在 const Craw = require('./libs/craw') 后边加上分号结尾,竟然好了;
由于不经常写node代码,对这个不是太熟悉;谨以此作为记录

浙公网安备 33010602011771号