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.
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?
From your config file, it seems like you're only excluding node_modules
from being parsed with babel-loader
, but not from being bundled.
In order to exclude node_modules
and native node libraries from bundling, you need to:
Add target: 'node'
to your webpack.config.js
. This will define NodeJs as the environment in which the bundle should run. For webpack, it changes the chunk loading behavior, available external modules and generated code style (ie. uses require()
for NodeJs) it uses during bundling.
Set the externalPresets
of node
to true
. As of Webpack@5, This configuration will exclude native node modules (path, fs, etc.) from being bundled.
Use webpack-node-externals in order to exclude other node_modules
.
So your result config file should look like:
var nodeExternals = require('webpack-node-externals');
...
module.exports = {
...
target: 'node', // use require() & use NodeJs CommonJS style
externals: [nodeExternals()], // in order to ignore all modules in node_modules folder
externalsPresets: {
node: true // in order to ignore built-in modules like path, fs, etc.
},
...
};
如果您在使用 TypeScript 时遇到此问题,您可能需要在tsconfig.json
文件中添加skipLibCheck: true
。
尝试使用绝对路径:
exclude:path.resolve(__dirname, "node_modules")
WebPack 1.x :
This was happening to me because I had specified my own resolve.extensions
but failed to include the empty extension ''
:
resolve: {
extensions: ['.js', '.jsx']
},
From the (no longer available!) webpack docs :
Setting this option will override the default, meaning that webpack will no longer try to resolve modules using the default extensions. If you want modules that were required with their extension (eg require('./somefile.ext')) to be properly resolved, you must include an empty string in your array. Similarly, if you want modules that were required without extensions (eg require('underscore')) to be resolved to files with “.js” extensions, you must include ".js" in your array.
Adding the empty extension resolved the errors:
resolve: {
extensions: ['', '.js', '.jsx']
},
试试下面的解决方案:
exclude:path.resolve(__dirname, "node_modules")
This worked for me:
exclude: [/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
include: A condition that must be met
loader: A string of "!" separated loaders
loaders: A array of loaders as string
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.