扩展 tsconfig 似乎被忽略了

编程入门 行业动态 更新时间:2024-10-05 05:24:26

扩展 tsconfig 似乎<a href=https://www.elefans.com/category/jswz/34/1624573.html style=被忽略了"/>

扩展 tsconfig 似乎被忽略了

我的应用程序根目录中有一个 tsconfig,它是由

create-react-app
创建的。在我的应用程序的 src 文件夹中,我有一个名为 api 的文件夹,其中包含 nodejs 服务器端代码。我遇到的问题是
create-react-app
的 tsconfig 不适用于我在服务器端代码中需要的内容。

{
  "compilerOptions": {
    "target": "es5",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react",
    "noImplicitAny": false,
    "downlevelIteration":true,
    "baseUrl": "src/",
  },
  "include": [
    "src"
  ],
  "exclude": [
    "api"
  ]
}

问题在于

"module": "esnext",
此选项仅适用于客户端代码,但不适用于节点,除非我理解正确的话,如果我有 .mjs 扩展名。在节点中,我需要将此选项设置为 commonjs。这导致我在运行代码时出现以下错误

SyntaxError: 不能在模块外使用 import 语句

所以我试图在 api 文件夹中创建一个新的 tsconfig 文件,它扩展了应用程序根目录的 tsconfig。这是代码。

{
  "extends": "../../tsconfig",
  "compilerOptions": {
    "module": "commonjs",
    "target": "es6",
    "noImplicitAny": true,
    "sourceMap": false,
    "noUnusedLocals": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
  }
}

但是感觉这完全被忽略了,因为我仍然得到完全相同的错误。

编辑:我添加了一个选项来排除根 tsconfig 中的 api 文件夹。还是不行。

有什么想法吗?

回答如下:

虽然这没有直接回答我的问题,但我确实设法通过给

ts-node
我希望它在运行服务器端代码时使用的 tsconfig 的确切路径来解决我的问题。这是它的样子。

"start:server": "./node_modules/.bin/ts-node --compiler typescript --project src/api/tsconfig.api.json src/api/index_server.ts"

更多推荐

扩展 tsconfig 似乎被忽略了

本文发布于:2024-05-30 07:32:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1770257.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:被忽略了   tsconfig

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!