3

在我的节点项目中,我babel-plugin-module-resolver曾经有相对路径。

tsconfig.json

{
  "compilerOptions": {
    "outDir": "build",
    "target": "es5",                          
    "module": "commonjs",                     
    "strict": true,     
    "noEmit": true,                    
    "esModuleInterop": true,                 
    "skipLibCheck": true,                    
    "forceConsistentCasingInFileNames": true,  
    "baseUrl": "./src",
    "paths": {
      "constants/*": ["constants/*"],
      "data/*": ["data/*"],
      "database/*": ["database/*"],
      "enums/*": ["enums/*"],
      "features/*": ["features/*"],
      "@library/*": ["library/*"],
    }
  }
}

.eslintrc

{
  "parser": "@typescript-eslint/parser",
  "extends": ["plugin:@typescript-eslint/recommended"],
  "parserOptions": {
    "ecmaVersion": 2018,
    "sourceType": "module"
  },
  "settings": {
    "import/resolver": {
      "babel-module": {}
    }
  },
  "rules": {
    "semi": ["warn", "always"],
    "quotes": ["warn", "single"],
    "max-len": ["warn", 150],
    "no-console": 1,
    "@typescript-eslint/explicit-function-return-type": "off",
    "@typescript-eslint/no-explicit-any": 0,
    "@typescript-eslint/no-inferrable-types": [
      "warn", {
        "ignoreParameters": true
      }
    ]
  }
}

.babelrc

{
  "presets": [
    "@babel/preset-typescript",
    "@babel/preset-env"
  ],
  "plugins": [ 
    [
      "module-resolver",
      {
        "alias": {
          "config": "./src/config",
          "constants": "./src/constants",
          "data": "./src/data",
          "enums": "./src/enums",
          "features": "./src/features",
          "library": "./src/library",
          "middleware": "./src/middleware",
          "utils": "./src/utils"
        }
      }
    ] 
  ]
}

当我导入文件时,它不会显示任何错误。可以通过单击导入路径移动到特定文件。但是当它遵守时,它会给出以下错误。在此处输入图像描述

如何解决这个问题?

4

1 回答 1

0

看起来您的 TS 配置文件paths和 Babel 配置文件的alias字段不匹配。@正如@jered 在问题评论中指出的那样,尝试为有问题的导入添加前缀。

正如@pasi 所说,您还可以查看https://github.com/TypeStrong/ts-node/issues/138以了解关于ts-nodeand的长时间讨论。paths

于 2021-03-25T05:59:24.413 回答