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
Webpack not excluding node_modules
提问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_modules
from being parsed with babel-loader
, but notfrom being bundled.
从您的配置文件中,您似乎只是排除node_modules
了被解析babel-loader
,而不是被捆绑。
In order to exclude node_modules
and native node libraries from bundling, you need to:
为了node_modules
从捆绑中排除本地节点库,您需要:
- Add
target: 'node'
to yourwebpack.config.js
. This will exclude native node modules(path, fs, etc.) from being bundled. - Use webpack-node-externalsin order to exclude other
node_modules
.
- 添加
target: 'node'
到您的webpack.config.js
. 这将从捆绑中排除本机节点模块(路径、fs 等)。 - 使用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: true
in your tsconfig.json
file.
如果您在使用 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