【问题标题】:webpack doesn't bundle node moduleswebpack 不捆绑节点模块
【发布时间】:2022-01-03 02:37:07
【问题描述】:

我想从 node_modules 中获取一个模块并且我想捆绑它(用于测试目的),但 Webpack 的行为就像它被添加到外部一样。

// no externals or any plugin used
let config = {
    mode: 'none',
    target: 'node',
    entry: {
      output: `/example.js`,
    },
    resolve: {
      extensions: ['.js'],
    },
    output: {
      path: './dist',
    },
  };
// exampl.js
require('path')
// dist/output.js
require('path');

预期行为

要捆绑的节点模块path

实际行为

Webpack 保持require('path');

【问题讨论】:

    标签: node.js webpack webpack-externals


    【解决方案1】:

    这是设计使然。当你在 webpack config 中设置 target: 'node' 时,webpack 不会捆绑 Node.js 的内置模块。 path 是 Node.js 的内置模块,并非来自 node_modules 目录。

    使用node webpack 将编译以在类似 Node.js 的环境中使用(使用 Node.js 需要加载块并且不接触任何内置模块,如 fs 或 path)。

    见targets

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-16
      • 1970-01-01
      • 2016-10-13
      • 2018-11-06
      • 2022-01-04
      • 1970-01-01
      • 2020-11-17
      • 1970-01-01
      相关资源
      最近更新 更多