问题
I am trying to run nightmare on the browser but I have learnt that it is not possible so I am trying to use webpack to bundle it and build it. When I am trying to build it with npx webpack --config webpack.config.js
,I am getting the following message :
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
this is my code of webpack.config.js
const path = require('path');
module.exports = {
entry: './src/index.js',
output: {
filename: 'main.js',
path: path.resolve(__dirname, 'dist'),
},
externals: ["fs"],
};
I don't know if this helps or not but previously I had faced some issues with resolving 'fs' The error message shown then was
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
This I had solved by installing fs seperately and adding the line externals: ["fs"],
to webpack.config.js
回答1:
Try setting the target to 'node' by adding these lines your webpack config:
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(),
}
来源:https://stackoverflow.com/questions/59381247/error-in-loading-nightmare-while-using-webpack