[英]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)
paths
was designed to help TypeScript understand path aliases used by bundlers paths
旨在帮助 TypeScript 理解打包程序使用的路径别名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:我试过的没有奏效:
tscpaths
.tscpaths
。 This package requires you to explicitly specify your project, src dir and out dir../build/src
instead of ./build
../build/src
而不是./build
之后,我让它工作了。 The caveat here: this package hasn't been touched since May 2019 as of writing this.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.