gpt4 book ai didi

javascript - typescript 不会替换 tsconfig 中定义的非相对路径

转载 作者:搜寻专家 更新时间:2023-10-30 21:01:53 25 4
gpt4 key购买 nike

我正在尝试使用自定义路径来简化常用模块的导入,我设置了这个配置:

// tsconfig.json
{
"compilerOptions": {
"module": "commonjs",
"noImplicitAny": false,
"removeComments": true,
"preserveConstEnums": true,
"outDir": "build",
"allowJs": true,
"target": "es5",
"sourceMap": true,
"baseUrl": ".",
"paths": {
"config": ["app/config"]
}

},
"exclude": [
"node_modules”,
"build"
]
}

我尝试使用“config”导入配置模块,但应用程序无法要求配置文件。编译文件里面的require路径还是“config”。

// result:
var config = require("config");
// what is should be:
var config = require("../../config");

竟然以为模块解析日志显示已经解析了。

======== Resolving module 'config' from '/abs/path/routes/internal/signin/index.ts'. ========
Module resolution kind is not specified, using 'NodeJs'.
'baseUrl' option is set to '/abs/path', using this value to resolve non-relative module name 'config'
'paths' option is specified, looking for a pattern to match module name 'config'.
Module name 'config', matched pattern 'config'.
Trying substitution 'config', candidate module location: 'config'.
Loading module as file / folder, candidate module location '/abs/path/config', target file type 'TypeScript'.
File '/abs/path/config.ts' does not exist.
File '/abs/path/config.tsx' does not exist.
File '/abs/path/config.d.ts' does not exist.
File '/abs/path/config/package.json' does not exist.
File '/abs/path/config/index.ts' exist - use it as a name resolution result.
======== Module name 'config' was successfully resolved to '/abs/path/config/index.ts'. ========

在编译后更改路径以指向正确的模块,我缺少什么?

最佳答案

您可以使用此代码修复非相对导入:obs:安装 tsconfig-paths 和 ts-node

"scripts": {
"start": "node -r tsconfig-paths/register -r ts-node/register ./dist/app.js",
}

关于javascript - typescript 不会替换 tsconfig 中定义的非相对路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43324209/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com