简体   繁体   English

使用 Webpack 捆绑原生 Node 模块

[英]Bundle native Node modules with Webpack

There are some node modules (for example the one named mssql/msnodesqlv8) which I cannot include in my webpack bundle, unless I specify the node-loader loader in the webpack config file (otherwise I get compilation errors).有一些节点模块(例如名为 mssql/msnodesqlv8 的模块)我不能包含在我的 webpack 包中,除非我在 webpack 配置文件中指定节点加载程序加载程序(否则我会得到编译错误)。

Now, my problem is that, if I use the config file listed below, I will not have one standalone main-bundle.js output file anymore, but webpack will also put a file named {some-guid-value}.node in the output folder, and the main-bundle.js app won't work without it.现在,我的问题是,如果我使用下面列出的配置文件,我将不再有一个独立的 main-bundle.js output 文件,但 webpack 也会在output 文件夹,没有它 main-bundle.js 应用程序将无法运行。

So my question is, how can I force webpack to output one single standalone bundle js file in this specific case?所以我的问题是,在这种特定情况下,如何强制 webpack 到 output 一个独立的捆绑 js 文件?

Here is my webpack.config.js file:这是我的 webpack.config.js 文件:

const path = require('path');

module.exports = {
    entry: './main.js',
    output: {
        filename: 'main-bundle.js',
        path: path.resolve(__dirname, 'dist'),
        libraryTarget: 'commonjs2'
    },
    target: 'node',
    module: {
        rules: [
            {
                test: /\.node$/,
                loader: 'node-loader',
            },
        ],
    },
};

And here is my package.json file:这是我的 package.json 文件:

{
  "name": "demo",
  "version": "1.0.0",
  "description": "",
  "main": "main.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "msnodesqlv8": "^2.0.8",
    "mssql": "^6.2.3"
  },
  "devDependencies": {
    "node-loader": "^1.0.2"
  }
}

Thanks!谢谢!

I am also struggling with the same context.我也在同样的背景下苦苦挣扎。 I have created my webpack based react redux config in that i cannot use fs, path, crypto which are node modules.我已经创建了基于 webpack 的反应 redux 配置,因为我不能使用节点模块的 fs、path、crypto。 But when i am using these modules inside create-react-app it works.但是当我在 create-react-app 中使用这些模块时,它可以工作。 I don;t know why it happens.我不知道为什么会这样。 I tried using the npm eject but wasn't able to figure out the specific config.我尝试使用 npm 弹出,但无法弄清楚具体的配置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM