[英]Babel module resolver doesn't work as expected (node + typescript)
在我的節點項目中,我使用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"
}
}
]
]
}
當我導入文件時,它不會顯示任何錯誤。 可以通過單擊導入路徑移動到特定文件。 但是當它遵守時,它會給出以下錯誤。
如何解決這個問題?
看起來您的 TS 配置文件的paths
和 Babel 配置文件的alias
字段不匹配。 正如@jered 在問題評論中指出的那樣,嘗試在有問題的導入上添加@
前綴。
正如@pasi 所說,您還可以看到https://github.com/TypeStrong/ts-node/issues/138進行關於ts-node
和paths
的長時間討論。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.