Gathering detailed insights and metrics for webpack-node-externals
Gathering detailed insights and metrics for webpack-node-externals
Gathering detailed insights and metrics for webpack-node-externals
Gathering detailed insights and metrics for webpack-node-externals
@types/webpack-node-externals
TypeScript definitions for webpack-node-externals
@yelo/rollup-node-external
Easily exclude node_modules in Rollup bundle, forked from webpack-node-externals
nuxt-node-externals-module
A nuxt module to add webpack-node-externals.
poi-webpack-node-externals
Easily exclude node_modules in Webpack bundle
npm install webpack-node-externals
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
1,296 Stars
125 Commits
61 Forks
9 Watching
13 Branches
9 Contributors
Updated on 20 Nov 2024
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
-3%
856,719
Compared to previous day
Last week
3.2%
4,465,653
Compared to previous week
Last month
16%
18,278,689
Compared to previous month
Last year
12.3%
187,465,762
Compared to previous year
Easily exclude node modules in Webpack
Webpack allows you to define externals - modules that should not be bundled.
When bundling with Webpack for the backend - you usually don't want to bundle its node_modules
dependencies.
This library creates an externals function that ignores node_modules
when bundling in Webpack.
(Inspired by the great Backend apps with Webpack series)
1npm install webpack-node-externals --save-dev
In your webpack.config.js
:
1const nodeExternals = require('webpack-node-externals'); 2... 3module.exports = { 4 ... 5 target: 'node', // in order to ignore built-in modules like path, fs, etc. 6 externals: [nodeExternals()], // in order to ignore all modules in node_modules folder 7 ... 8};
And that's it. All node modules will no longer be bundled but will be left as require('module')
.
Note: For Webpack 5, in addition to target: 'node'
also include the externalsPreset
object:
1// Webpack 5 2 3const nodeExternals = require('webpack-node-externals'); 4... 5module.exports = { 6 ... 7 target: 'node', 8 externalsPresets: { node: true }, // in order to ignore built-in modules like path, fs, etc. 9 externals: [nodeExternals()], // in order to ignore all modules in node_modules folder 10 ... 11};
This library scans the node_modules
folder for all node_modules names, and builds an externals function that tells Webpack not to bundle those modules, or any sub-modules of theirs.
This library accepts an options
object.
options.allowlist (=[])
An array for the externals
to allow, so they will be included in the bundle. Can accept exact strings ('module_name'
), regex patterns (/^module_name/
), or a function that accepts the module name and returns whether it should be included.
Important - if you have set aliases in your webpack config with the exact same names as modules in node_modules, you need to allowlist them so Webpack will know they should be bundled.
options.importType (='commonjs')
The method in which unbundled modules will be required in the code. Best to leave as commonjs
for node modules.
May be one of documented options or function callback(moduleName)
which returns custom code to be returned as import type, e.g:
1options.importType = function (moduleName) { 2 return 'amd ' + moduleName; 3}
options.modulesDir (='node_modules')
The folder in which to search for the node modules.
options.additionalModuleDirs (='[]')
Additional folders to look for node modules.
options.modulesFromFile (=false)
Read the modules from the package.json
file instead of the node_modules
folder.
Accepts a boolean or a configuration object:
1{ 2 modulesFromFile: true, 3 /* or */ 4 modulesFromFile: { 5 fileName: /* path to package.json to read from */, 6 includeInBundle: [/* whole sections to include in the bundle, i.e 'devDependencies' */], 7 excludeFromBundle: [/* whole sections to explicitly exclude from the bundle, i.e only 'dependencies' */] 8 } 9}
1var nodeExternals = require('webpack-node-externals'); 2... 3module.exports = { 4 ... 5 target: 'node', // important in order not to bundle built-in modules like path, fs, etc. 6 externals: [nodeExternals({ 7 // this WILL include `jquery` and `webpack/hot/dev-server` in the bundle, as well as `lodash/*` 8 allowlist: ['jquery', 'webpack/hot/dev-server', /^lodash/] 9 })], 10 ... 11};
For most use cases, the defaults of importType
and modulesDir
should be used.
Webpack allows inserting regex in the externals array, to capture non-relative modules:
1{ 2 externals: [ 3 // Every non-relative module is external 4 // abc -> require("abc") 5 /^[a-z\-0-9]+$/ 6 ] 7}
However, this will leave unbundled all non-relative requires, so it does not account for aliases that may be defined in webpack itself.
This library scans the node_modules
folder, so it only leaves unbundled the actual node modules that are being used.
Using the allowlist
option, this is possible. We can simply tell Webpack to bundle all files with extensions that are not js/jsx/json, using this regex:
1...
2nodeExternals({
3 // load non-javascript files with extensions, presumably via loaders
4 allowlist: [/\.(?!(?:jsx?|json)$).{1,5}$/i],
5}),
6...
Thanks @wmertens for this idea.
When writing a node library, for instance, you may want to split your code to several files, and use Webpack to bundle them. However - you wouldn't want to bundle your code with its entire node_modules dependencies, for two reasons:
As a consumer of a library, I want the library code to include only its logic, and just state its dependencies so they could me merged/resolved with the rest of the dependencies in my project. Bundling your code with your dependencies makes it virtually impossible.
In short: It's useful if your code is used by something that has dependencies managed by npm
Contributions and pull requests are welcome. Please run the tests to make sure nothing breaks.
1npm run test
MIT
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 0/17 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
31 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More