Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用Web包时加载噩梦时出错_Javascript_Webpack_Nightmare - Fatal编程技术网

Javascript 使用Web包时加载噩梦时出错

Javascript 使用Web包时加载噩梦时出错,javascript,webpack,nightmare,Javascript,Webpack,Nightmare,我试图在浏览器上运行噩梦,但我知道这是不可能的,所以我尝试使用webpack捆绑并构建它。当我尝试使用npxwebpack--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.j

我试图在浏览器上运行噩梦,但我知道这是不可能的,所以我尝试使用webpack捆绑并构建它。当我尝试使用
npxwebpack--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,并在webpack.config.js中添加了行
externals:[“fs”],

尝试将目标设置为“node”,方法是在您的webpack配置中添加以下行:

const path = require('path');

module.exports = {
  entry: './src/index.js',
  output: {
    filename: 'main.js',
    path: path.resolve(__dirname, 'dist'),
  },
  externals: ["fs"],
};
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(),
}

没有线索。。尝试
externals:[“fs”,“child\u process”],
没有错误,但在控制台中运行时仍然存在错误,但在浏览器上执行实际程序时,我得到了这个未捕获的引用错误:fs未定义这防止了终端上的任何错误,但在运行程序时,它现在在控制台上显示了这一点
在main.js中未定义require
babel loader编译它。等一下,你是在浏览器中运行吗?是的,我正在尝试在浏览器中运行它,即使这些仍然会导致相同的错误