“解析错误:未检测到 babel 配置文件”当 Vue 项目根目录下的 IDE 未打开时

发布于 2025-01-10 00:13:57 字数 345 浏览 0 评论 0原文

我偶然发现了一个问题,在 VS Code 中,当创建 Vue 项目并且未在 Vue 项目的根目录中打开时,babel.config.js 不会加载,并且 IDE 会混淆 babel 配置的位置。 运行时出错vue 项目不在根目录

我的所有文件在读取任何 javascript/vue 文件的第一个字符时都显示错误 未检测到 [#]...的 Babel 配置文件...或配置 babel,以便它可以找到配置文件。

I stumbled into a problem where in VS Code, when a Vue project is created and not open at root directory of the Vue project, babel.config.js wouldn't load and IDE would be confused as to where the babel config is.
error when running vue project not at root

All of my files show an error on the first character of any javascript/vue file reading
No Babel config file detected for [#]... or configure babel so that it can find the config files.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

风吹雪碎 2025-01-17 00:13:57

将块添加到 settings.json 将解决此问题:

"eslint.workingDirectories": [
        {"mode": "auto"}
 ],

要访问 settings.json 文件,请单击 Ctrl+, 或从“文件 >”>首选项>设置,
然后在搜索栏中输入 eslint,在选项中找到在 settings.json 中编辑

Adding the block to settings.json will solve this issue:

"eslint.workingDirectories": [
        {"mode": "auto"}
 ],

To access settings.json file, click Ctrl+, or from, File > Preferences > Settings,
then type eslint in the search bar, find Edit in settings.json in Options.

°如果伤别离去 2025-01-17 00:13:57

有两种方法可以解决这个问题,对我来说100%有效。

我正在使用react.js。但我成功解决了这个问题。我认为这个解决方案会对您有所帮助。

我尝试过将 requireConfigFile 设置为 false 或在 .eslintrc.js 中、在 .babelrc (或 Babel 配置文件),以及 package.json 中的“babel”,都没有效果。

方法 1 - 将此代码添加到 .eslintrc.js

.eslintrc.js

"parser": '@babel/eslint-parser',
"parserOptions": {
    "requireConfigFile": false,
}

方法 2 - 安装此包 @babel/core

npm i --save-dev @babel/core

<强>.babelrc

{
    "presets": ["@babel/preset-env", "@babel/preset-react"],
    "plugins": ["@babel/plugin-proposal-optional-chaining"]
}

Two ways fix this issue, It's worked me 100%.

I'm using react.js. But I successfully fixed this issue. I think this solution will be helpful for you.

I've variously attempted to either set requireConfigFile to false or to create some sort of Babel config, in an .eslintrc.js, in a .babelrc (or Babel Configuratio file), and in a "babel" in package.json, all to no effect.

Method 1 - add this codes into .eslintrc.js

.eslintrc.js

"parser": '@babel/eslint-parser',
"parserOptions": {
    "requireConfigFile": false,
}

Method 2 - install this package @babel/core

npm i --save-dev @babel/core

.babelrc

{
    "presets": ["@babel/preset-env", "@babel/preset-react"],
    "plugins": ["@babel/plugin-proposal-optional-chaining"]
}
勿忘初心 2025-01-17 00:13:57

https://babeljs.io/docs/en/config-files
Babel 期望您的配置文件位于根级别,因此为了消除 IDE 的混乱,您需要为 VSCodes 扩展创建一个 eslint 设置。在 vscode-eslint 设置下,切换到顶部选项卡上的工作区,然后滚动到:

Eslint:选项

eslint 选项对象,用于提供从命令行执行时通常传递给 eslint 的参数(请参阅 https://eslint.org/docs/developer-guide/nodejs-api#eslint-class)。

在 settings.json 中编辑 <-单击该

Vs code 将创建一个 .vscode/ 文件夹,在其中创建 settings.json 文件。添加这一行:

{
    "eslint.options": {
        "configFile": "\\ABSOLUTE\\PATH\\TO\\YOUR\\PROJECT\\VUE_PROJECT\\babel.config.js"
    }
}

这将告诉 IDE 做什么。

https://babeljs.io/docs/en/config-files
Babel expects your config file to be at root level, so in order to un-confuse your IDE you need to create an eslint setting for VSCodes extention. Under vscode-eslint settings switch to workspace on the top tab, then scroll to:

Eslint: Options

The eslint options object to provide args normally passed to eslint when executed from a command line (see https://eslint.org/docs/developer-guide/nodejs-api#eslint-class).

Edit in settings.json <-click on that

Vs code will make a .vscode/ folder inside which a settings.jsonfile was created. There add this line:

{
    "eslint.options": {
        "configFile": "\\ABSOLUTE\\PATH\\TO\\YOUR\\PROJECT\\VUE_PROJECT\\babel.config.js"
    }
}

This will tell the IDE what to do.

叹倦 2025-01-17 00:13:57

在应用程序中创建一个名为 .eslintrc.js 的文件
并粘贴此代码:

module.exports = {
  extends: 'eslint-config-antife',
  plugins: [
    "babel",
    "html",
  ]
}

Create a file inside your application with name .eslintrc.js
and paste this code:

module.exports = {
  extends: 'eslint-config-antife',
  plugins: [
    "babel",
    "html",
  ]
}
时光无声 2025-01-17 00:13:57

在我的小 Vue 应用程序中,

在我的 package.json 中添加:“requireConfigFile”:false 就可以了!

 },
  "eslintConfig": {
    "root": true,
    "env": {
      "node": true
    },
    "extends": [
      "plugin:vue/essential",
      "eslint:recommended"
    ],
    "parserOptions": {
      "parser": "@babel/eslint-parser",
      "requireConfigFile":false
    },
    "rules": {}
  },

In my little Vue app,

Add : "requireConfigFile":false in my package.json made it Alright!

 },
  "eslintConfig": {
    "root": true,
    "env": {
      "node": true
    },
    "extends": [
      "plugin:vue/essential",
      "eslint:recommended"
    ],
    "parserOptions": {
      "parser": "@babel/eslint-parser",
      "requireConfigFile":false
    },
    "rules": {}
  },
君勿笑 2025-01-17 00:13:57

转到您的 .eslintrc.js 文件。在里面你会发现“parserOptions”对象。在那里添加 requireConfigFile: false

parserOptions: {
   //if there's an existing code here, do not change it.
   requireConfigFile: false, //just add this line
},

Go to your .eslintrc.js file. Inside you will find the "parserOptions" object. Add requireConfigFile: false there:

parserOptions: {
   //if there's an existing code here, do not change it.
   requireConfigFile: false, //just add this line
},
笔落惊风雨 2025-01-17 00:13:57

当我修改 package.json 中的一些脚本设置时,我得到了这个。不确定我犯了什么错误:我刚刚撤消了该文件中的所有输入,然后一切又开始工作了。

I got this when I amended some of the script settings in package.json. Not precisely sure what mistake I made: I just undid all typing in this file, and things started working again.

分開簡單 2025-01-17 00:13:57

再次从 Visual Studio 安装 ES lint。这个问题将会得到解决。首先卸载 ES Lint,然后再次安装 ES Lint。

Again install ES lint from Visual Studio. This problem will be solve. First uninstall ES Lint and then again install ES Lint.

梦冥 2025-01-17 00:13:57

运行 linter 命令时,请确保您位于项目的正确目录中。这就是我的情况。

Make sure you are on the right directory of the project when you run your linter commands. That was my case.

痴者 2025-01-17 00:13:57

这个简单的修复对我有用...

获取 babel.config.json 的相对路径

右键单击 babel.config.json 文件,然后在上下文菜单“复制相对路径”中添加此路径到我的 .eslintrc.js 中的 configFile 属性

.eslintrc.js

...
  parser: "@babel/eslint-parser",
  parserOptions: {
    sourceType: 'module',
    babelOptions: {
      configFile: 'aaa/bbb/ccc/babel.config.json' // <- workspace relative path
    }
  },
...

禁用 &重新启用插件

然后只需为您的工作区禁用 vscode-eslint 插件,然后重新启用它。不再有红色波浪线。

This simple fix worked for me...

Get relative path to babel.config.json

Right-click on the babel.config.json file and in the context menu 'Copy Relative Path' then added this path to my configFile property in the .eslintrc.js

.eslintrc.js

...
  parser: "@babel/eslint-parser",
  parserOptions: {
    sourceType: 'module',
    babelOptions: {
      configFile: 'aaa/bbb/ccc/babel.config.json' // <- workspace relative path
    }
  },
...

Disable & Re-enable Plugin

Then just disable the vscode-eslint plugin for your workspace then re-enable it. No more red squiggly lines.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文