I have a library that can be used with both node.js and the browser. I am using CommonJS then publishing for the web version using webpack. My code looks like this:
In order to exclude node_modules and native node libraries from being bundled, you need to:
target: 'node' to your webpack.config.js. This will exclude native node modules (path, fs, etc.) from being bundled.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
...
};