Yaron
Yaron

Reputation: 1965

How to fix webpack Error: Cannot find module - on node.js (webpackEmptyContext)

My webpack.config.js is :

const path = require('path');

module.exports = {
  entry: './app.ts',
  target: 'node',
  node: {
    __dirname: true
  },
  module: {
    rules: [
      {
        test: /\.tsx?$/,
        use: 'ts-loader',
        exclude: /node_modules/,
      },
    ],
  },
  resolve: {
    extensions: [ '.tsx', '.ts', '.js' ],
  },
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'dist'),
  },
};

When I build my project and run it I am getting the following error:

(node:51071) UnhandledPromiseRejectionWarning: Error: Cannot find module '../gather/gatherers/css-usage'
    at webpackEmptyContext (webpack:///./node_modules/lighthouse/lighthouse-core/config_sync?:2:10)
    at Function.requireGathererFr

This error is raised from a 3rd party library I am using - lighthouse.

How to fix this error ?

Upvotes: 2

Views: 5484

Answers (1)

tmhao2005
tmhao2005

Reputation: 17474

For target as node, you might have to use this package webpack-node-externals in order to ignore all modules in node_module folder. Then declare as externals:

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

module.exports = {
  externals: [nodeExternals()],
  // ...
};

Upvotes: 3

Related Questions