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. 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.
In my code below, it's not working. common.browser is undefined in 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
const puppeteer = require('puppeteer')- try typing here./puppeteer, you're missing./doSomething.print() //<-- returns undefinedIs .print undefined, or does it return undefined ? Because if the function print returns undefined, its normal.returnanything from a function, it returnsundefined.