我使用 puppeteer-extra
和一些 puppeteer-extra-plugins
制作了一个 nodeJS 应用程序,调试源代码工作正常。当我使用 https://www.npmjs.com/package/pkg 将其打包到 .exe
文件中时,它也可以正常运行。
但是,当 .exe 移动到与“输出”目录不同的目录时,我收到错误
Cannot find module 'debug'
1) If you want to compile the package/file into executable, please pay attention to compilation warnings
and specify a literal in 'require' call. 2) If you don't want to compile the package/file into executable
and want to 'require' it from filesystem (likely plugin), specify an absolute path in 'require' call
using process.cwd() or process.execPath.
我从不使用 debug
模块,我的某些依赖项必须在内部需要它...但是我该如何处理这个错误?
我有这些要求
const puppeteer = require(path.join(process.cwd(), 'dependencies', 'puppeteer-extra'));
const StealthPlugin = require(path.join(process.cwd(), 'dependencies', 'puppeteer-extra-plugin-stealth'));
const RecaptchaPlugin = require(path.join(process.cwd(), 'dependencies', 'puppeteer-extra-plugin-recaptcha'));
const UserPrefsPlugin = require(path.join(process.cwd(), 'dependencies', 'puppeteer-extra-plugin-user-preferences'));
和我的 package.json(pkg 的配置)
"pkg": {
"scripts": [
"node_modules/puppeteer/lib/*.js"
],
"assets": [
"./node_modules/@types",
"./node_modules/typescript/lib/*.d.ts",
"src/**/*.ts",
"./tsconfig.json"
],
"targets": [
"node8-win32"
]
}
答案 0 :(得分:0)
我通过改变我的要求来解决它
const puppeteer = require('puppeteer-extra');
const RecaptchaPlugin = require('puppeteer-extra-plugin-recaptcha');
const UserPrefsPlugin = require('puppeteer-extra-plugin-user-preferences');
以及稍后启动浏览器,在 puppeteer 的选项中明确设置 executablePath。
const options = {
headless: false,
devtools: false,
args: ['--start-maximized'],
timeout: 300000,
downloadPath: path.join(process.env.USERPROFILE, 'Downloads'),
askDownload: false,
executablePath: path.join(process.cwd(), "puppeteer-extra",
"node_modules\\puppeteer\\.local-chromium\\win64-722234\\chrome-win\\chrome.exe")
}
await puppeteer.launch(this.options)
但是请注意,我不得不忽略'puppeteer-extra-plugin-stealth',因为它抱怨
<块引用>"列出了一个插件 'puppeteer-extra-plugin-stealth/evasions/chrome.runtime' as 依赖,目前缺少。请安装它:"
并按照建议添加 pkg/asset here,没有帮助