【问题标题】:Babel module resolver doesn't work as expectedBabel 模块解析器无法按预期工作
【发布时间】:2023-03-17 09:10:01
【问题描述】:

我在我的节点项目中使用 eslint 配置了 babel-plugin-module-resolver。这是我的 eslintrc。

{
  "env": {
    "es6": true,
    "node": true
  },
  "extends": [
    "airbnb-base"
  ],
  "globals": {
    "Atomics": "readonly",
    "SharedArrayBuffer": "readonly"
  },
  "parserOptions": {
    "ecmaVersion": 2018,
    "sourceType": "module"
  },
  "settings": {
    "import/resolver": {
      "babel-module": {}
    }
  },
  "rules": {
    "class-methods-use-this": "off",
    "newline-per-chained-call": "off",
    "indent": ["warn", 2],
    "quotes": ["warn", "single"],
    "no-unused-vars": ["error", {"warn": "none"}],
    "max-len": ["warn", 150],
    "no-console": 1,
    "object-curly-newline": ["warn", {
      "ObjectPattern": { "multiline": true },
      "ExportDeclaration": { "multiline": true, "minProperties": 4 }
    }]
  }
}

这是我的 babelrc 文件。

{   
  "presets": ["@babel/preset-env"],
  "plugins": [
    [
      "module-resolver",
      {
        "alias": {
          "constants": "./constants",
          "database": "./database",
          "library": "./library",
          "mappers": "./mappers",
          "middleware": "./middleware",
          "models": "./models",
          "repositories": "./repositories",
          "routes": "./routes",
          "services": "./services",
          "utils": "./utils"
        }
      }
    ]
  ] 
} 

现在在我的项目中,路径可以像这样正常工作。

import User from 'models/user.model';
import UserRepository from 'repositories/user.repository';

但我有以下问题。 当我单击导入时,它不会转到定义。 IntelliSense 不工作。

我在这里用这些插件做什么?我想要智能感知并转到定义(导航)功能。

【问题讨论】:

  • 我也遇到了同样的问题,你解决了吗?
  • 是的。你是在使用 node 还是 react?
  • React Native 需要 nodeJs

标签: javascript node.js babeljs eslintrc


【解决方案1】:

@shashika,您必须为 javascript 项目设置 jsconfig.json,为 typescript 项目设置 tsconfig.json 以使您的自定义路径转到各自的文件定义,

例子:

    {
     "compilerOptions": {
       "baseUrl": ".",
       "paths": {
         "@/*": ["./src/*"],
         "@util/*": ["./util/*"],
       },
    }
  

【讨论】:

    猜你喜欢
    • 2021-06-13
    • 2012-04-20
    • 2018-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-25
    相关资源
    最近更新 更多