【问题标题】:absolute path in the tsconfig dose not worktsconfig 中的绝对路径不起作用
【发布时间】:2020-12-23 22:46:57
【问题描述】:

我看到一些关于this problem的问题,没有一个不起作用 我有一个 nodejs 项目和 Typescript。我不喜欢使用相对路径。在 tsconfig 中设置路径时出现以下错误:

找不到模块'@app/controllers/main'

// main.ts
export const fullName = "xxxx";
...

// app.ts
import { fullName } from '@app/controllers/main'
...

这是我项目的结构:

-node_modules
-src
----controllers
---------------main.ts
----app.ts
-package.json
-tsconfig.json

tsconfig:

{
    "compilerOptions": {
        "target": "es5",
        "module": "commonjs",
        "strict": true,
        "baseUrl": ".",
        "paths": {
            "@app/*": ["src/*"]
        },
        "esModuleInterop": true,
        "skipLibCheck": true,
        "forceConsistentCasingInFileNames": true
    }
}

我的问题在哪里?

提前致谢。

【问题讨论】:

  • 您是如何收到错误的?当您构建代码或 TSC 编译器时显示在 IDE 上?
  • 当我想用这个命令运行项目时:ts-node-dev src/index.ts
  • 有什么理由不使用纯tsc 代替吗?

标签: node.js typescript tsconfig tsconfig-paths


【解决方案1】:

你的语法是正确的。

在撰写本文时,ts-node-dev 似乎不支持 pathstsconfig.json 条目。

github issue 讨论了该问题并提供了解决方法。

【讨论】:

    【解决方案2】:

    很遗憾(我也不知道为什么)Typescript 编译器目前不能很好地支持路径转换。

    这是我的解决方案:

    我在this 项目中使用了该解决方案。

    安装 devDependencies

    1. ttypescript -> npm install ttypescript --save-dev -> TTypescript(Transformer TypeScript)通过动态修补编译模块以使用来自 tsconfig 的转换器来解决问题.json。
    2. typescript-transform-paths -> npm install typescript-transform-paths --save-dev -> 将绝对导入从 tsconfig.json 中的路径转换为相对。
    3. tsconfig-paths -> npm install tsconfig-paths --save-dev -> 使用它来加载位置在 tsconfig.json 的路径部分中指定的模块。支持在运行时加载和通过 API 加载。
    4. ts-node-dev -> npm install ts-node-dev --save-dev -> 当任何所需文件发生变化时,它会重新启动目标节点进程(作为标准 node-dev ) 但在重启之间共享 Typescript 编译过程

    tsconfig.json

    使用以下选项更新 tsconfig.json 文件:

    {
        "compilerOptions": {
            /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
            "paths": {
                "@app/*": [
                    "./src/*"
                ]
            },
            /* Advanced Options */
            "plugins": [
                {
                    "transform": "typescript-transform-paths"
                }
            ],
        }
    }
    

    构建

    在编译阶段使用 ttsc 而不是 tsc 和配置文件。请参阅下面的 sn-p:

    npx ttsc --p ./tsconfig.json
    

    带自动重载的开发模式

    当您处于开发模式时,使用以下脚本(将其放在 package.json 中的脚本选项中)以使用正确的路径自动重新加载项目。 src/app.ts 是您的应用程序的“入口点”,位于 src 文件夹下。

    npx ts-node-dev --prefer-ts true --no-notify -r tsconfig-paths/register --watch src --transpileOnly src/app.ts
    

    PS:使用 ts-node-dev 可以显着提高速度。

    【讨论】:

    • 路径从未打算转换输出中的模块说明符的原因。相反,它旨在描述此类转换,正如其他工具对语言执行的类型检查一样。路径的典型子用例包括 RequireJS config.paths、SystemJS config.{packages|paths|map} 和 Webpack module.resolve.{alias|modules}。此外,TypeScript 避免在输出中重写模块说明符,原因有整个主机
    【解决方案3】:

    我稍微调整了@Carlo Corradini 的回答, 这是我解决此问题的方法。

    1. 安装所需的插件: npm i -D ttypescript typescript-transform-paths ts-node tsconfig-paths。这些包将帮助我们改变路径。
      ttypescript-> https://www.npmjs.com/package/ttypescript

    2. 然后,在 tsconfig.json 上,我输入:

      {
      
       "ts-node": {
         "transpileOnly": true,
         "require": [  // set this so you dont need to use ts-node -r 
           "typescript-transform-paths/register",
           "tsconfig-paths/register"
          ]
       },         
       "compilerOptions": {
          "composite": true,
          "rootDir": ".", // must define
          "baseUrl": "src", // must define, the paths will relative to this
          "outDir": "lib", 
          "skipLibCheck": false,
          "paths": {
           "@app/*": ["./*"],
           "@controllers/*": ["./controllers/*"],
          },
           "plugins": [
             { "transform": "typescript-transform-paths" }
           ]
       }
      }
      
    3. 然后在您的代码上您可以使用:

      import myControllers from "@controllers/main.ts"
      
    4. 现在你可以编译了,npx ttsc -p tsconfig.json

    5. 要在 ts-node 上运行,请使用 npx ts-node -p tsconfig.json src/app.ts

    【讨论】:

      【解决方案4】:

      我在 tsconfig.json 文件的某个地方看到了这个:

      { 
        "compilerOptions": {
          ...other rules,
          "baseUrl": "./src"
        },
        "include": ["src"]
      }
      

      成功了

      【讨论】:

      • 您好,欢迎来到 Stack Overflow!请拨打tour。感谢您的回答,但您是否还可以添加有关您的代码如何解决问题的解释?另请查看help center 以获取有关如何格式化代码的信息。
      猜你喜欢
      • 2022-11-29
      • 2018-11-13
      • 2013-07-06
      • 1970-01-01
      • 2018-03-28
      • 1970-01-01
      • 2015-08-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多