0

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
10
  • Try adding .js to each of the files, at least that's the way I do it, also try require_once Commented Sep 21, 2017 at 14:29
  • const puppeteer = require('puppeteer') - try typing here ./puppeteer, you're missing ./ Commented Sep 21, 2017 at 14:30
  • doSomething.print() //<-- returns undefined Is .print undefined, or does it return undefined ? Because if the function print returns undefined, its normal. Commented Sep 21, 2017 at 14:30
  • @FrançoisP. is right - if you don't return anything from a function, it returns undefined. Commented Sep 21, 2017 at 14:32
  • @FrançoisP. common.browser is undefined Commented Sep 21, 2017 at 14:32

2 Answers 2

1

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();
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.