[英]How to split code in multiple files in Node.js?
I'm trying to split my code in multiple files but it's not working and I'm not sure why. 我正在尝试将我的代码拆分为多个文件,但无法正常工作,我不确定为什么。
I have 3 files, main.js, common.js and doSomething.js. 我有3个文件,main.js,common.js和doSomething.js。
common.browser
is a chrome instance so it's important that it only gets launched once and that I can access it from every file. common.browser
是chrome实例,因此重要的是它只能启动一次,并且我可以从每个文件访问它。
In my code below, it's not working. 在下面的代码中,它不起作用。
common.browser
is undefined in doSomething.print()
common.browser
在doSomething.print()
未定义
//File 1: main.js
(async() => {
const common = require('./common')
const doSomething = require('./doSomething')
await common.init()
doSomething.print() //<-- prints 'undefined'
})()
//File 2: common.js
const puppeteer = require('puppeteer')
let common = {}
common.init = async () => {
common.browser = await puppeteer.launch()
}
module.exports = common
//File3: doSomething.js
const common = require('./common')
let doSomething = {}
const browser = common.browser //<-- Added this and it makes it not work.
doSomething.print = () => {
console.log(browser)
}
module.exports = doSomething
In you common.js
file you are setting this.browser = await puppeteer.launch()
here, the keyword this
does not refer to the object common
. 在
common.js
文件中,您在此处设置this.browser = await puppeteer.launch()
,关键字this
并不指向common
对象。
You could simply use the object common. 您可以简单地使用通用对象。
//File 2: common.js
const puppeteer = require('puppeteer')
let common = {}
common.init = async () => {
common.browser = await puppeteer.launch()
}
module.exports = common
Or if you want to use this
, you must give common a constructor and instantiate it. 或者,如果要使用
this
,则必须给common提供一个构造函数并实例化它。
const puppeteer = require('puppeteer')
const common = function() {}
common.prototype.init = async function() {
this.browser = await puppeteer.launch()
};
module.exports = new common()
Same as before with class syntax (you need node 8.xx) 与之前的类语法相同(您需要节点8.xx)
const puppeteer = require('puppeteer')
class Common {
constructor() {}
async init() {
this.browser = await puppeteer.launch();
}
}
module.exports = new Common();
I was testing with the solution and I was getting an error. 我正在测试解决方案,但出现错误。 I could make it work adding "async" in the file 3. I changed the names of the files, and I added some data, sorry about that.
我可以通过在文件3中添加“异步”来使其工作。我更改了文件名,并添加了一些数据,对此感到抱歉。
// file 1: index.js
(async () => {
const common = require('./common')
const actions = require('./actions')
await common.init()
actions.web() //<-- open google.com in browser
})()
// file 2: common.js
const puppeteer = require('puppeteer')
let common = {}
common.init = async () => {
common.browser = await puppeteer.launch({
headless: false,
slowMo: 50 // slow down by 50ms
//devtools: true
})
common.page = await common.browser.newPage()
}
module.exports = common
// file 3: actions.js
const common = require('./common')
let actions = {}
actions.web = async () => {
await common.page.goto('https://google.com', {
waitUntil: 'networkidle2'
})
}
module.exports = actions
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.