无法使用 import 而不是 require,或者无法使用 ts-node 和 tsdx 构建依赖项

Unable to use import instead of require, or unable to build dependensides with ts-node & tsdx

我有以下问题,我知道它被触发的确切原因,但我还没有找到任何解决方案,所以社区可能会帮助我解决这个问题。

我正在使用 tsdx 来启动 ts repos,并且还使用 ts-node 来进行快速调试(而不是每次都构建代码)。

递归困境:

所以,每次我想通过 yarn add modulename 将模块添加到我的包时,如果在我的 tsconfig.json 文件中的 compilerOptions 选项中: "module": "commonjs" 有以下值,tsdx build 无法构建代码,return 我出现以下错误:

Error: Incompatible tsconfig option. Module resolves to 'CommonJS'. This is incompatible with rollup, please use 'module: "ES2015"' or 'module: "ESNext"'

但是!

如果我将 compilerOptionscommonjs 更新为 module: "ESNext"ts-node 会给我一个错误,因为它仍然无法使用 import {Method} from Module const x = require(CommonJS)(根据 this issue and ,这是一个很老的错误)

  "compilerOptions": {
    "module": "commonjs", || "ESNext"
    "lib": ["dom", "esnext"],
    ...//other options

据我所知,我正在使用来自 ts-node 存储库的 WebStorm 和 README.md,我可以使用自定义 tsconfig 更新 Environment Variables所以在那种情况下,我需要在 repo 中有两个 tsconfig 文件,用于 ts-node 和生产 。所以问题是:也许还有另一个 TS_NODE_FLAG,尤其是 module 编译器选项?所以我可以 运行 带有标志的 ts-node,这会覆盖这个次要参数吗?

目前的妥协方案是有两个完全相同的配置文件:

  • tsconfig.json
  • tsconfig.dev.json

除了第二个有"module":"commonjs"

第二个选项实际上 对我不起作用 是从 .

添加到 WebStrom 的环境变量 TS_NODE_COMPILER_OPTIONS='{"module":"commonjs"}'