如何修复webpack错误:在node.js(webpacketmptycontext)上找不到模块

如何修复webpack错误:在node.js(webpacketmptycontext)上找不到模块,node.js,webpack,Node.js,Webpack,我的webpack.config.js是: const path = require('path'); module.exports = { entry: './app.ts', target: 'node', node: { __dirname: true }, module: { rules: [ { test: /\.tsx?$/, use: 'ts-loader', exclude: /no

我的
webpack.config.js
是:

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'),
  },
};
当我构建并运行我的项目时,我得到以下错误:

(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
此错误来自我正在使用的第三方库-
lighthouse


如何修复此错误?

对于目标as节点,您可能必须使用此软件包
网页包节点外部
,以便忽略
节点\模块
文件夹中的所有模块。然后声明为
externals

const nodeExternals=require('webpack-node-externals');
module.exports={
外部:[nodeExternals()],
// ...
};