在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代碼,對這個不是太熟悉;謹以此作為記錄