简体   繁体   English

typescript 4.4.4:tsconfig 路径未按预期解析

[英]typescript 4.4.4: tsconfig paths not resolving as expected

my tsconfig.json我的 tsconfig.json

{
  "compilerOptions": {
    "target": "ES2018",
    "module": "CommonJS",
    "lib": ["ESNext", "ESNext.AsyncIterable"],
    "skipLibCheck": true,
    "sourceMap": true,
    "outDir": "./dist",
    "moduleResolution": "node",
    "removeComments": true,
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "noImplicitThis": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "resolveJsonModule": true,
    "baseUrl": "./",
    "paths": {
      "@/*": ["src/*"],
      "@config": ["src/config"],
    }
  },
  "exclude": ["node_modules"],
  "include": ["./src/**/*.ts"]
}

I am pretty sure there is nothing wrong in tsconfig paths because I even get path autocompletion from my IDE.我很确定 tsconfig 路径没有任何问题,因为我什至可以从我的 IDE 获得路径自动完成功能。 however when my code is compiled (tsc --project tsconfig.json)但是当我的代码被编译时(tsc --project tsconfig.json)

import { PRODUCTION } from '@config';

// or

import { PRODUCTION } from '@/config';

I get following javascript我得到关注 javascript

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const _config_1 = require("@config");
console.log(_config_1.PRODUCTION);
//# sourceMappingURL=index.js.map

but javascript doesn't understand what "@" is therefore I get "module not found" error.但是 javascript 不明白“@”是什么,因此我收到“找不到模块”错误。 How can I solve this problem?我怎么解决这个问题?

See this issue on TypeScript's GitHub project: Module path maps are not resolved in emitted code (#10866)请参阅 TypeScript 的 GitHub 项目上的此问题:发出的代码中未解析模块路径映射 (#10866)

tl;dr tl;博士

  • TypeScript won't rewrite your module paths TypeScript 不会重写你的模块路径
  • paths was designed to help TypeScript understand path aliases used by bundlers paths旨在帮助 TypeScript 理解打包程序使用的路径别名
  • You'll either need to:你要么需要:
    • use a bundler, such as webpack , and configure its own path maps使用打包器,比如webpack ,并配置它自己的路径映射
    • use a build time tool, such as tspath , to rewrite the paths使用构建时工具(例如tspath )重写路径
    • modify the runtime module resolver, with something like module-alias使用module-alias 之类的东西修改运行时模块解析器

I've been stuck with this issue for 2 whole days trying to find something that works.我已经被这个问题困扰了整整 2 天,试图找到可行的方法。 @Wing's answer is correct, if you're using plain tsc with no other tools, then Node won't know what you mean by @/ (or whatever your alias is) in your imports. @Wing 的答案是正确的,如果您使用纯tsc而没有其他工具,那么 Node 将不知道您在导入时使用的@/ (或您的别名是什么)是什么意思。

I've tried a few different approaches and I've found one that required no configuration.我尝试了几种不同的方法,我找到了一种不需要配置的方法。

What worked :什么有效

At the end of my journey I came across a package that is still actively being maintained: tsc-alias .在我的旅程结束时,我遇到了一个仍在积极维护中的 package: tsc-alias The only thing I needed to do was add && tsc-alias -p tsconfig.json to my tsc command in my build script and voilà, it works.我唯一需要做的就是在我的构建脚本中将&& tsc-alias -p tsconfig.json添加到我的tsc命令中,瞧,它可以工作了。 So your full command would be something like tsc -p tsconfig.json && tsc-alias -p tsconfig.json .因此,您的完整命令将类似于tsc -p tsconfig.json && tsc-alias -p tsconfig.json

What I tried that didn't work:我试过的没有奏效:

  • I tried "module-alias" and I couldn't get it to work.我尝试了“模块别名”,但无法正常工作。
  • I tried "tsconfig-paths" as suggested by @AlexMorley-Finch with no luck as well.我按照@AlexMorley-Finch 的建议尝试了“tsconfig-paths”,但也没有运气。 The package seems to be finding the modules but there seems to be a bug that causes the path of the modules not to be mapped correctly (there's an issue open for it). package 似乎正在寻找模块,但似乎有一个错误导致模块的路径无法正确映射(存在一个问题)。
  • I then came across tscpaths .然后我遇到了tscpaths This package requires you to explicitly specify your project, src dir and out dir.这个 package 要求你明确指定你的项目,src dir 和 out dir。 I got it to work after figuring out my outdir needed to be ./build/src instead of ./build .在确定我的 outdir 需要是./build/src而不是./build之后,我让它工作了。 The caveat here: this package hasn't been touched since May 2019 as of writing this.此处需要注意的是:截至撰写本文时,自 2019 年 5 月以来尚未触及此 package。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM