Javascript Webpack 不排除 node_modules

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33001237/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 14:29:16  来源:igfitidea点击:

Webpack not excluding node_modules

javascriptwebpack

提问by ndugger

I'm using webpack for a Node framework that I'm building (though I should probably use gulp, admittedly). When I include the EJS module, webpack includes it in the compiled source, even though I explicitly tell it to exclude the node_modules dir.

我正在将 webpack 用于我正在构建的 Node 框架(尽管我可能应该使用 gulp,无可否认)。当我包含 EJS 模块时,webpack 会将它包含在编译源中,即使我明确告诉它排除 node_modules 目录。

module.exports = {
    context: __dirname,
    target: 'node',
    // ...
    output: {
        libraryTarget: 'commonjs'
        // ...
    },
    module: {
        loaders: [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                loader: 'babel-loader?{ "stage": 0, "optional": ["runtime"] }'
            }
        ]
    }
};

As you can see, I have a test for JS files, and I tell it to exclude node_modules; why is it ignoring my exclude?

如您所见,我对 JS 文件进行了测试,并告诉它排除 node_modules;为什么它忽略了我的排除?

回答by lyosef

From your config file, it seems like you're only excluding node_modulesfrom being parsed with babel-loader, but notfrom being bundled.

从您的配置文件中,您似乎只是排除node_modules了被解析babel-loader而不是被捆绑。

In order to exclude node_modulesand native node libraries from bundling, you need to:

为了node_modules从捆绑中排除本地节点库,您需要:

  1. Add target: 'node'to your webpack.config.js. This will exclude native node modules(path, fs, etc.) from being bundled.
  2. Use webpack-node-externalsin order to exclude other node_modules.
  1. 添加target: 'node'到您的webpack.config.js. 这将从捆绑中排除本机节点模块(路径、fs 等)。
  2. 使用webpack-node-externals来排除其他node_modules.

So your result config file should look like:

所以你的结果配置文件应该是这样的:

var nodeExternals = require('webpack-node-externals');
...
module.exports = {
    ...
    target: 'node', // in order to ignore built-in modules like path, fs, etc. 
    externals: [nodeExternals()], // in order to ignore all modules in node_modules folder 
    ...
};

回答by mattnedrich

If you ran into this issue when using TypeScript, you may need to add skipLibCheck: truein your tsconfig.jsonfile.

如果您在使用 TypeScript 时遇到此问题,则可能需要添加skipLibCheck: true到您的tsconfig.json文件中。

回答by Alan

Try use absolute path:

尝试使用绝对路径:

exclude:path.resolve(__dirname, "node_modules")

回答by Sina Dorostkar

try this below solution:

试试下面的解决方案:

exclude:path.resolve(__dirname, "node_modules")

回答by freecks

This worked for me:

这对我有用:

exclude: [/bower_components/, /node_modules/]

排除:[/bower_components/, /node_modules/]

module.loaders

模块加载器

A array of automatically applied loaders.

一系列自动应用的加载器。

Each item can have these properties:

每个项目都可以具有以下属性:

test: A condition that must be met

测试:必须满足的条件

exclude: A condition that must not be met

exclude:不能满足的条件

include: A condition that must be met

包括:必须满足的条件

loader: A string of "!" separated loaders

loader:一串“!” 分离装载机

loaders: A array of loaders as string

loaders: 加载器数组作为字符串

A condition can be a RegExp, an absolute path start, or an array of one of these combined with "and".

条件可以是一个正则表达式、一个绝对路径起点,或者是其中一个与“和”组合的数组。

See http://webpack.github.io/docs/configuration.html#module-loaders

http://webpack.github.io/docs/configuration.html#module-loaders