导入时baseUrl和node_modules冲突

Conflict between baseUrl and node_modules when importing

我有一个配置如下的 TS 项目:

tsconfig.json(部分)

{
  "compilerOptions": {
    "module": "commonjs",
    "baseUrl": "src",
    "esModuleInterop": true,
  },
  "include": [
    "src"
  ]
}

我依赖于 stripe NPM 包:

{
  // ...
  "dependencies": {
    "stripe": "^8.45.0",
  }
}

然后我有以下文件:

src/routes/payments/index.ts
src/stripe/index.ts

我在 src/routes/payments/index.ts 中遇到一些导入问题。我想导入 stripe 库,而不是我自己的代码。

有效:

// Uses me the 'Stripe' constructor which is the default export from the package
const stripe = require('stripe')('KEY');

这不起作用:

import Stripe from 'stripe';
const stripe = new Stripe('KEY');

我收到以下错误:

Module '"PROJECT/src/stripe/index"' has no default export.

如何消除歧义并告诉 TS 我想使用 node_modules 中的 stripe

你能尝试像这样更新 tsconfig.json 文件吗:

{
 "compilerOptions": {
    "module": "commonjs",
    "esModuleInterop": true,
    "baseUrl": ".",
    "paths": {
      "*": [
        "node_modules/*",
        "src/*"
      ]
    }
  }
}