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

时间:2017-04-10 13:15:39

标签: javascript node.js typescript

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

// 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'. ========

编译后指向正确的模块时,路径是如何更改的?

2 个答案:

答案 0 :(得分:0)

Apparently paths was never intended to actually resolve the url to its relative version。您应该使用某种后处理器来执行此操作。我正在使用this Babel plugin

答案 1 :(得分:0)

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

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