node.js - 如何修复 webpack 错误:在 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
此错误是从我正在使用的第 3 方库引发的 - lighthouse
。
如何解决此错误?
解决方案
对于作为节点的目标,您可能必须使用此包webpack-node-externals
才能忽略node_module
文件夹中的所有模块。然后声明为externals
:
const nodeExternals = require('webpack-node-externals');
module.exports = {
externals: [nodeExternals()],
// ...
};
推荐阅读
- python - 取消任务时异步锁死锁
- elasticsearch - 在不同服务器中执行 bash 脚本的 logstash
- sql - 使用新创建的用户登录 postgres
- java - org.hibernate.MappingException - 复合 id
- batch-file - 批处理文件:如何检查 %~dp0 是否为 UNC 网络路径?
- c++ - 将 SPI 数据从 C 传递到 CPP Teensy 3.5
- perl - Perl - 使用打印功能时得到意外结果
- r - 如何创建具有由为每一行返回不同值的函数获得的值的向量?
- anylogic - 安装旧版本的 AnyLogic 或降级 AnyLogic
- google-cloud-platform - Bitbucket 使用 terraform 源 repo 镜像