javascript - 使用 webpack 时加载 Nightmare 时出错

标签 javascript webpack nightmare

我试图在浏览器上运行 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/

相关文章:

javascript - 如何使用颜色选择器作为 HTML 输入元素

javascript - 在javascript中将变量值转换为字符串

javascript - Webpack2 不理解我的 SASS 文件中的 @import 语句(How to compile SASS with webpack2?)

symfony - Datatables.net (1.10.22) + Webpack Encore (1.11) + Symfony (5.2.6)

typescript - 编译 TypeScript,目标设置为 'ES3' 或 'ES5' 内部

javascript - Nightmarejs 同一测试中的多个页面

node.js - Nightmare 般的网络抓取循环,每次输出都不相同

javascript - 页面加载时的 .get() 请求

javascript - 如何通过javascript在浏览器中显示python输出?

javascript - 如何在 Node 上进行 Nightmare 般的条件循环点击事件?