簡體   English   中英

打字稿不替換 tsconfig 中定義的非相對路徑

[英]typescript do not replace non-relative paths defined in tsconfig

我試圖使用自定義路徑來簡化常用模塊的導入,我設置了這個配置:

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

在編譯后更改路徑以指向正確的模塊時,我缺少什么?

顯然, paths從未打算將 url 實際解析為其相對版本 您應該使用某種后處理器來執行此操作。 我正在使用這個 Babel 插件

您可以使用此代碼來修復非相對導入: obs: install tsconfig-paths 和 ts-node

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM