节点需要函数内部未定义常量

问题描述 投票:0回答:1

我正在 Node (commonjs) 中工作,我有一个

const
,我正在用
require
module.exports
抓取它,它在一个函数中运行良好,但是一旦我进入函数中的一个函数,它突然间就变得不确定了。

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: '[email protected]',
  password: 'some_pass',
};

module.exports = SRLoginCredentials;

package.json

"type": "commonjs",

任何关于为什么会发生这种情况的见解将不胜感激!

javascript node.js commonjs
1个回答
0
投票

@danh 在上面的评论中完美回答了我的问题!

© www.soinside.com 2019 - 2024. All rights reserved.