簡體   English   中英

擴展 tsconfig 似乎被忽略了

[英]extending tsconfig seems to be getting ignored

我的應用程序根目錄中有一個 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"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM