最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

扩展 tsconfig 似乎被忽略了

网站源码admin36浏览0评论

扩展 tsconfig 似乎被忽略了

扩展 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"

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论