cooldude101
cooldude101

Reputation: 1405

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. 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

Upvotes: 0

Views: 3813

Answers (2)

coyr
coyr

Reputation: 689

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.

// 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

Upvotes: 1

Francois
Francois

Reputation: 3080

In you common.js file you are setting this.browser = await puppeteer.launch() here, the keyword this does not refer to the object 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.

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)

const puppeteer = require('puppeteer')

class Common {
    constructor() {}

    async init() {
        this.browser = await puppeteer.launch();
    }
}

module.exports = new Common();

Upvotes: 1

Related Questions