I want to use WebPack to bundle backend code. Per the suggestion of other stackoverflow answers, I used the config file described here:
var webpack = require('webpack');
var path = require('path');
var fs = require('fs');
var nodeModules = {};
fs.readdirSync('node_modules')
.filter(function(x) {
return ['.bin'].indexOf(x) === -1;
})
.forEach(function(mod) {
nodeModules[mod] = 'commonjs ' + mod;
});
module.exports = {
entry: './src/main.js',
target: 'node',
output: {
path: path.join(__dirname, 'build'),
filename: 'backend.js'
},
externals: nodeModules
}
but even after reading the article explaining why we have to list all modules in node_module
as external, I am still confused why we have to have this step and why without it, we get this error:
WARNING in ./~/express/lib/view.js
Critical dependencies:
Particularly can someone explain this paragraph and how specifically require('express')
in src/main.js
is an example of this problem:
But there is a problem. Webpack will load modules from the node_modules folder and bundle them in. This is fine for frontend code, but backend modules typically aren't prepared for this (i.e. using require in weird ways) or even worse are binary dependencies. We simply don't want to bundle in anything from node_modules.
I want to better understand this step in WebPack so I am not just blindly copying the config file and being complacent that it works.
Thanks