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

标签 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'. ========

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

最佳答案

您可以使用此代码修复非相对导入: 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/

相关文章:

javascript - 防止鼠标单击事件触发特定 z-index 上的元素

javascript - 使用 javascript 根据 data-html 将类附加到元素

node.js - Hue API 远程基本身份验证

javascript - KeyValue 和 AutoComplete 的 Angular Filter Pipe

javascript - 如何在 TypeScript 类中扩展 ES6 类

javascript - JS 文件中的 TypeScript 语法 - 可能吗?

javascript - 禁用父级单击但保持子级单击事件处于事件状态

javascript - Babel 文件被复制而不被转换

javascript - nodejs https错误: socket hang up with localhost

node.js - 等待有 promise 的事件