如何摆脱 Typescript + Node 18 中的相对导入?

问题描述 投票:0回答:1

我目前正在 Typescript 5.2 中运行后端应用程序,可以将其构建为在 Node 18 的生产环境中作为 Debian 服务运行,或者通过 nodemon 直接在 Docker 容器中运行。

在我的应用程序中的任何地方,您都可以找到相对导入,有时很短,有时很长:

import { Shortcut } from './Shortcut';
import { formatQuery } from '../../../../../../helpers/formatQuery';

我想摆脱这些进口。


我的

package.json
看起来像:

{
  "name": "toto",
  "version": "1.4.9",
  "description": "tata",
  "scripts": {
    "start:dev": "nodemon --config ./node_modules/@titi/config/nodemon.json",
    "build": "tsc -p tsconfig.json"
  },
  "devDependencies": {
    "jest": "29.6.1",
    "ts-jest": "29.1.0",
    "typescript": "5.2.2"
  },
  "dependencies": {
    "nodemon": "3.0.1",
  },
  "volta": {
    "node": "18.17.0",
    "yarn": "1.22.17"
  }
}

我使用自定义的

nodemon.json
,即:

{
  "watch": ["src/", "*.json"],
  "ignore": [
    ".git",
    "public",
    "node_modules/**/node_modules",
    "src/**/*.test.*",
    "src/**/*.spec.*"
  ],
  "ext": "ts,js,json",
  "exec": "tsc -p tsconfig.json --incremental && node --trace-warnings --trace-uncaught build || exit 1"
}

最后,我的

tsconfig.json
文件如下所示:

{
  "compilerOptions": {
    "lib": ["es2020"],
    "module": "commonjs",
    "target": "es2020",
    "moduleResolution": "node",
    "outDir": "../../../build",
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "forceConsistentCasingInFileNames": true,
    "allowSyntheticDefaultImports": true,
    "downlevelIteration": true,
    "declaration": true,
    "sourceMap": true,
    "pretty": true,
    "baseUrl": ".",
    "paths": {
      "@services/*": [
        "src/services/*"
      ],
      "@routes/*": [
        "src/routes/*"
      ],
      "@helpers/*": [
        "src/helpers/*"
      ]
    },
  },
  "files": ["../../../src/index.ts"],
}

已经知道确实存在很多解决方案,我已经尝试了很多,但没有一个有效,例如:

  • 设置
    baseUrl
    属性

无论我将其设置为

.
还是
./src
中的
tsconfig.json
,我都无法启动我的项目并且不断收到此错误:

import { formatQuery } from 'src/helpers';

src/services/toto.ts:5:29 - error TS2307
:找不到模块“src/helpers”或其相应的类型声明。

  • paths
    属性添加到
    tsconfig.json

每当我设置

paths
属性时,它都会被忽略。例如:

"@helpers/*": [ "src/helpers/*" ]

并将像这样的结果导入到:

import { formatQuery } from '@helpers';

src/services/toto.ts:5:29 - error TS2307
:找不到模块“@helpers”或其相应的类型声明。

(即使我把

@helpers/formatQuery
,甚至用
BaseUrl
.

  • 使用
    NODE_PATH
    环境变量

由于

BaseUrl
paths
属性更改了已编译的文件(javascript),因此您不能指望 JavaScript 知道 BaseUrl 在哪里,所以我尝试在
nodemon.json
的“exec”中使用此环境变量,例如:

"exec": "tsc -p tsconfig.json --incremental && NODE_PATH=src node --trace-warnings --trace-uncaught build || exit 1"
  • 更改我的 IDE 设置(针对 JS 和 TS)

我目前正在使用 VSCode,我在某处读到我需要将

typescript.preferences.importModuleSpecifier
变量编辑为
non-relative
(即使我不喜欢这种方式,因为它全局意味着将在我的项目上工作的新开发人员)如果他们不在 VSCode 中设置此属性,将无法使其工作?)

我尝试添加一个带有

jsconfig.json
baseUrl
属性的
paths
文件,如放入
tsconfig.json
中,但没有成功。


那么我做错了什么?我的架构是否太复杂而无法使用非相对导入?

typescript tsconfig absolute-path relative-import tsconfig-paths
1个回答
0
投票

rootDir
属性添加到
tsconfig.json
并对
baseUrl
paths
进行一些小更改:

"rootDir": "./src",
"baseUrl": "./src",
"paths": {
  "@helpers/*": [ "./helpers/*" ],
}

在路径上,您不需要添加

src
,因为
baseUrl
正在创建从此文件夹开始的路径。如果您添加它,那么
ts
将搜索
/src/src/....
,但情况并非如此。阅读更多这里

© www.soinside.com 2019 - 2024. All rights reserved.