我编写了一个使用jwks-rsa npm软件包的打字稿模块。
module.ts
const jwksClient = require('jwks-rsa');
const client = jwksClient({
...myoptions
});
export myfunc = () => {
// do something with jwksClient
}
然后在我的处理程序中。
import {myfunc} from './handler.ts'
....
myfunc()
....
当开玩笑地测试module.ts时,我没有任何错误,一切都很好。
Typescript项目使用以下支架:
serverless create --template aws-nodejs-typescript
问题是我使用webpack构建打字稿后,当我运行使用上述处理程序的应用程序路由时.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'),],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/**/*"
]
}