Error in loading nightmare while using webpack
Asked Answered
H

1

1

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

Homecoming answered 17/12, 2019 at 19:55 Comment(2)
no clue.. try externals: ["fs", "child_process"],Remand
there was no error but still on running it in the console but while executing the actual program on the browser I was getting this Uncaught ReferenceError: fs is not definedHomecoming
C
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(),
}
Comeuppance answered 17/12, 2019 at 20:5 Comment(4)
This prevented any errors on the terminal but on running the program it shows this on the console now require is not defined in main.jsHomecoming
You need to use babel-loader to compile it.Comeuppance
Wait a minute, are you running in the browser?Comeuppance
yes i am trying to run it in browser and even these still lead to the same errorHomecoming

© 2022 - 2024 — McMap. All rights reserved.