如何修复Webpack错误:找不到模块-on node.js(webpackEmptyContext)

亚龙

我的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

我正在使用-的3rd party库中引发此错误lighthouse

如何解决这个错误?

tmhao2005

对于目标作为节点,可能必须使用此软件包webpack-node-externals才能忽略node_module文件夹中的所有模块然后声明为externals

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章