4

I have a Node js server app which uses Express and Pug. I would like to bundle it to single script which can be deployed by pm2. There seem to be several problems with this.

  1. In runtime I get Cannot find module "." and during compilation few messages like

WARNING in ./node_modules/express/lib/view.js 80:29-41 Critical dependency: the request of a dependency is an expression

appear which come from dynamic imports like require(mod).__express. I assume Webpack can't statically resolve those and does not know which dependency to include.

How can this be solved ?

  1. How do I make Pug compile and be part of the output js ?
Graham
  • 7,431
  • 18
  • 59
  • 84
ps-aux
  • 11,627
  • 25
  • 81
  • 128

1 Answers1

1

It is because webpack rebundle node_modules (already bundled) dependencies and in the case of pug, it doesn't work.

You need to use webpack-node-externals within the webpack externals option in order to specifically ask not to re-bundle depedencies.

  1. Install webpack-node-externals: npm i -D webpack-node-externals
  2. Integrate it your webpack config file:

Example

// ...

const nodeExternals = require('webpack-node-externals')

module.exports = {
  target: 'node',

  entry: {
    // ...
  },

  module: {
    // ...
  },

  externals: [nodeExternals()],

  output: {
    // ...
  },
}
Ivan Gabriele
  • 6,433
  • 5
  • 39
  • 60