Express, pug and webpack

I have a Node js server application that uses Express

and Pug

. I would like to link it to a single script that can be deployed pm2

. There seem to be several problems with this.

  • At runtime I get Cannot find module "."

    and at compile time several messages like

WARNING c. / node_modules / express / lib / view.js 80: 29-41 Critical dependency: The dependency request is an expression

which come from dynamic imports such as require(mod).__express

. I suppose that Webpack cannot statically solve these problems and doesn't know which dependency to include.

How can this be solved?

  1. How do I Pug

    compile and be part of the js output?
+3


source to share


1 answer


This is because webpack is restoring node_modules

(already bundled) dependencies, and in the case of pug, it doesn't work.

You need to use webpack-node-externals in your webpack option externals

to specifically ask not to reinstall dependencies.

  • Install webpack-node-externals: npm i -D webpack-node-externals

  • Integrate it into your webpack config file:


Example

// ...

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

module.exports = {
  target: 'node',

  entry: {
    // ...
  },

  module: {
    // ...
  },

  externals: [nodeExternals()],

  output: {
    // ...
  },
}

      

0


source







All Articles