我试图在浏览器上运行 Nightmare ,但我知道这是不可能的,所以我尝试使用 webpack 来捆绑它并构建它。当我尝试使用 npx webpack --config webpack.config.js 构建它时,我收到以下消息:
WARNING in ./node_modules/nightmare/lib/nightmare.js 332:20-336:2
Critical dependency: the request of a dependency is an expression
@ ./src/index.js
ERROR in ./node_modules/nightmare/lib/nightmare.js
Module not found: Error: Can't resolve 'child_process' in '/home/subhang23/Desktop/congenial-journey/node_modules/nightmare/lib'
@ ./node_modules/nightmare/lib/nightmare.js 17:11-35
@ ./src/index.js
这是我的 webpack.config.js 代码
const path = require('path');
module.exports = {
entry: './src/index.js',
output: {
filename: 'main.js',
path: path.resolve(__dirname, 'dist'),
},
externals: ["fs"],
};
我不知道这是否有帮助,但之前我在解决“fs”时遇到过一些问题 当时显示的错误消息是
ERROR in ./node_modules/nightmare/lib/nightmare.js
Module not found: Error: Can't resolve 'child_process' in '/home/subhang23/Desktop/congenial-journey/node_modules/nightmare/lib'
@ ./node_modules/nightmare/lib/nightmare.js 17:11-35
@ ./src/index.js
ERROR in ./node_modules/electron/index.js
Module not found: Error: Can't resolve 'fs' in '/home/subhang23/Desktop/congenial-journey/node_modules/electron'
@ ./node_modules/electron/index.js 1:9-22
@ ./node_modules/nightmare/lib/nightmare.js
@ ./src/index.js
ERROR in ./node_modules/nightmare/lib/actions.js
Module not found: Error: Can't resolve 'fs' in '/home/subhang23/Desktop/congenial-journey/node_modules/nightmare/lib'
@ ./node_modules/nightmare/lib/actions.js 8:9-22
@ ./node_modules/nightmare/lib/nightmare.js
@ ./src/index.js
ERROR in ./src/index.js
Module not found: Error: Can't resolve 'fs' in '/home/subhang23/Desktop/congenial-journey/src'
@ ./src/index.js 14:15-28
我通过单独安装 fs 并将行 externals: ["fs"],
添加到 webpack.config.js 解决了这个问题
最佳答案
尝试通过在 webpack 配置中添加这些行来将目标设置为“node”:
const nodeExternals = require('webpack-node-externals');
const path = require('path');
module.exports = {
entry: './src/index.js',
output: {
filename: 'main.js',
path: path.resolve(__dirname, 'dist'),
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
},
},
],
},
target: 'node',
externals: nodeExternals(),
}
关于javascript - 使用 webpack 时加载 Nightmare 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59381247/