【问题标题】:Error in loading nightmare while using webpack使用 webpack 加载噩梦时出错
【发布时间】:2020-04-10 08:52:48
【问题描述】:

我正在尝试在浏览器上运行 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 解决了这个问题

【问题讨论】:

  • 没有线索.. 试试externals: ["fs", "child_process"],
  • 没有错误,但仍在控制台中运行它,但在浏览器上执行实际程序时,我得到了这个 Uncaught ReferenceError: fs is not defined

标签: javascript webpack nightmare


【解决方案1】:

尝试通过在 webpack 配置中添加这些行来将目标设置为“节点”:

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(),
}

【讨论】:

  • 这防止了终端上的任何错误,但是在运行程序时,它现在在控制台上显示了这一点 require is not defined in main.js
  • 需要使用 babel-loader 编译。
  • 等一下,你在浏览器中运行吗?
  • 是的,我正在尝试在浏览器中运行它,即使这些仍然会导致相同的错误
猜你喜欢
  • 1970-01-01
  • 2012-02-01
  • 1970-01-01
  • 2010-12-21
  • 2020-03-14
  • 2010-11-13
  • 2010-12-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多