typescript - 为什么我得到带有 jwks-rsa 模块的 webpackMissingModule?
问题描述
我编写了一个使用 jwks-rsa npm 包的打字稿模块。
模块.ts
const jwksClient = require('jwks-rsa');
const client = jwksClient({
...myoptions
});
export myfunc = () => {
// do something with jwksClient
}
然后在我的 handler.ts
import {myfunc} from './handler.ts'
....
myfunc()
....
用 jest 测试 module.ts 时,我没有收到任何错误,一切正常。
打字稿项目是脚手架,使用:
serverless create --template aws-nodejs-typescript
问题是在我使用 webpack 构建 typescript 之后,当我运行使用上述 handler.ts -> myFunc() 代码的应用程序路由时,我收到以下错误:
{ Error: Cannot find module './JwksClient'
at webpackMissingModule (webpack-internal:///./node_modules/jwks-rsa/lib/index.js:3:83)
at eval (webpack-internal:///./node_modules/jwks-rsa/lib/index.js:3:170)
在 /jwks-rsa/lib/index.js:3:170 中:
var _JwksClient = require('./JwksClient');
我相信我应该以某种方式调整 webpack.config.js ......但还没有任何线索......
webpack.config.js
const path = require('path');
const slsw = require('serverless-webpack');
const nodeExternals = require('webpack-node-externals');
module.exports = {
context: __dirname,
mode: slsw.lib.webpack.isLocal ? 'development' : 'production',
entry: slsw.lib.entries,
devtool: slsw.lib.webpack.isLocal ? 'cheap-module-eval-source-map' : 'source-map',
resolve: {
extensions: ['.mjs', '.json', '.ts'],
symlinks: false,
cacheWithContext: false,
},
output: {
libraryTarget: 'commonjs',
path: path.join(__dirname, '.webpack'),
filename: '[name].js',
},
target: 'node',
externals: [nodeExternals()],
module: {
rules: [
// all files with a `.ts` or `.tsx` extension will be handled by `ts-loader`
{
test: /\.(tsx?)$/,
loader: 'ts-loader',
exclude: [
[
path.resolve(__dirname, 'node_modules'),
path.resolve(__dirname, '.serverless'),
path.resolve(__dirname, '.webpack'),
],
],
options: {
transpileOnly: true,
experimentalWatchApi: true,
},
},
],
},
};
tsconfig.js
{
"compilerOptions": {
"lib": ["es2017"],
"removeComments": true,
"moduleResolution": "node",
"noUnusedLocals": true,
"noUnusedParameters": true,
"sourceMap": true,
"target": "es2017",
"outDir": "lib",
},
"include": ["./**/*.ts", "env.local.js"],
"exclude": [
"node_modules/**/*",
".serverless/**/*",
".webpack/**/*",
"_warmup/**/*",
".vscode/**/*"
]
}
解决方案
推荐阅读
- reactjs - 多个导航栏 REACTJS
- python - 使用 Haarcascade 检测到的人脸,如何在不同颜色的相同人脸上绘制边界框
- python - 在 C++ 中估计嵌入式 python 脚本的进度
- javascript - 图像不在画布内旋转
- sql - 如何在分区级别查看分区表的 I/O 统计信息
- javascript - 我试图在单击按钮时禁用/启用下拉菜单
- python - Python 使用 DocxTemplate 填充 docx 表
- java - Android中的HTTPRequest线程
- visual-studio-code - 突出显示 VSCode 中的自定义代码部分
- amazon-web-services - 带有签名的响应 403 与使用 presignurl s3 不匹配