ps-aux
ps-aux

Reputation: 12146

Express, Pug and Webpack

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 ?

Upvotes: 4

Views: 1330

Answers (1)

Ivan Gabriele
Ivan Gabriele

Reputation: 6900

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: {
    // ...
  },
}

Upvotes: 1

Related Questions