简体   繁体   English

VSCode 无法正确解析 Intellisense 的 babel 绝对导入

[英]VSCode not resolving babel absolute imports correctly for Intellisense

I am losing my mind over this.我正在为此失去理智。 I have a very simple hello world project that I want to use Babel and ESLint in. I also want to use absolute imports, but vscode is not resolving my absolute imports correctly.我有一个非常简单的 hello world 项目,我想在其中使用 Babel 和 ESLint。我也想使用绝对导入,但是 vscode 没有正确解析我的绝对导入。

My project structure is:我的项目结构是:

- node_modules
- src
  - config
    - index.js
  - logger
    - index.js
  index.js
- package.json
- .babelrc
- .eslintrc
- ...

src/index.js

import express from "express";
import config from "config";
import logger from "logger";

// Constants
const { port, host } = config;

// App
const app = express();
app.get("/", (req, res) => {
  res.send("Hello World");
});

app.listen(port, host);
logger.info(`Running on http://${host}:${port}`);

My code runs, eslint is not erroring and everything works fine, but vscode IntelliSense is not working properly!我的代码运行,eslint 没有出错,一切正常,但是 vscode IntelliSense 不能正常工作! It's resolving to some TypeScript package not my actual file!它解决了一些 TypeScript package 不是我的实际文件!

vscode智能感知不工作

jsconfig.json

{
  "module": "es6",
  "moduleResolution": "node",
  "compilerOptions": {
    "resolveJsonModule": true,
    "module": "commonjs",
    "target": "es6",
    "allowSyntheticDefaultImports": true,
    "moduleResolution": "node",
    "baseUrl": ".",
    "paths": {
      "*": ["./src/*"]
    }
  },
  "exclude": ["node_modules", "**/node_modules/*"]
}

.babelrc

{
  "presets": ["@babel/preset-env"],
  "plugins": [
    [
      "module-resolver",
      {
        "root": ["./src"]
      }
    ]
  ]
}

.eslintrc

{
  "extends": "airbnb",
  "plugins": ["prettier"],
  "settings": {
    "import/resolver": {
      "node": {
        "paths": ["src"]
      }
    }
  },
  "rules": {
    "quotes": ["warn", "double"]
  }
}

In jsconfig.json file:jsconfig.json文件中:

"paths": {
      "*/*": ["src/*"]
    }

In .babelrc file, install babel-plugin-module-resolver and change to:.babelrc文件中,安装babel-plugin-module-resolver并更改为:

"plugins": [
    [
      "module-resolver",
      {
        "root": ["./src"],
        "alias": {
          "*": "./src"
        }
      }
    ]
  ]

Result here结果在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM