首页 > 解决方案 > 为什么我得到带有 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/**/*"
  ]
}

标签: typescriptwebpackserverless

解决方案


推荐阅读