javascriptnode.jscommonjs

Node require constant undefined inside function


I am working in Node (commonjs) and I have a const that I am grabbing with require and module.exports and it is coming through fine within one function but once I'm inside a function in the function, it is all of a sudden undefined.

main.js

const SRLoginCredentials = require('./secrets.js');

const puppeteer = require('puppeteer-extra');

// Add stealth plugin and use defaults
const pluginStealth = require('puppeteer-extra-plugin-stealth');
const { executablePath } = require('puppeteer');

// Use stealth
puppeteer.use(pluginStealth());

// Launch puppeteer-stealth
puppeteer.launch({ headless: false, executablePath: executablePath() }).then(async (browser) => {
  // Create a new page
  const page = await browser.newPage();

  // Go to the website
  await page.goto('https://google.com');

  // Wait for security check
  await page.waitForTimeout(2000);

  await page.waitForSelector('#APjFqb');
  console.log(SRLoginCredentials);            // SRLoginCredentials is defined
  
  await page.$eval('#email', (el) => (console.log(SRLoginCredentials));            // SRLoginCredentials is undefined


  await browser.close();
});

secrets.js

const SRLoginCredentials = {
  email: 'my.email@gmail.com',
  password: 'some_pass',
};

module.exports = SRLoginCredentials;

package.json

"type": "commonjs",

Any insight into why that's happening would be much appreciated!


Solution

  • @danh answered my question perfectly in a comment above!