解析错误:未检测到Babel配置文件。

29

我正在尝试创建一个Vue项目。但是在创建项目后,我遇到了以下问题:

Parsing error: No Babel config file detected for C:\\HotelManagmet\clienthotel\babel.config.js. Either disable config file checking with requireConfigFile: false, or configure Babel so that it can find the config files. 我无法解决这个问题,我已经尝试重新安装Node.js和Visual Studio Code,但问题仍然存在。尝试运行npm audit fix --force,但结果仍然相同。有人知道可能是什么原因吗?

5个回答

46

我遇到了同样的问题,这个更改解决了问题。 在你的 .eslintrc.js 文件中,添加 requireConfigFile: false

parserOptions: {
  parser: '@babel/eslint-parser',
  requireConfigFile: false, // <== ADD THIS
  ecmaVersion: 2018, // Allows for the parsing of modern ECMAScript features
  sourceType: 'module' // Allows for the use of imports
}

11
注意:parser: '@babel/eslint-parser' 需要在 parserOptions 之外定义。 - Saibamen
嘿,@pradeep,如果我们没有这样的.eslintrc.js文件怎么办?在Vue3中,默认情况下,在package.json文件中进行eslint配置。 - Vitomir
1
@Vitomir https://babeljs.io/docs/configuration#packagejson - henhen
@Saibamen 我觉得这取决于 eslint 的版本,我不得不在 parserOptions 中定义 parser - undefined
在Vue 3中,需要在package.json的parseOperation部分中添加"requireConfigFile": false - undefined

7

解决方案1

最重要的是:babel.config.js 应该在你的根目录下。 也就是说,如果你有这样的结构:

enter image description here
root directory
    --your project1
    |
    |--src
    |--nodemodules
    |--babel.config.js
    --your project2
    |
    |--src
    |--nodemodules
    |--babel.config.js

如果您运行项目,它会显示错误。所以如果您想要运行项目1或项目2,只需打开它并确保它是根目录。 解决方案2 打开.eslintrc.js文件,并将根目录的值更改为:
root:'',

4
 "parserOptions": {
      "parser": "@babel/eslint-parser",
      "requireConfigFile": false // add this in package.json for Vue3
    },

1
如果你有一个babel配置文件或者想要定义一个,你可以将它链接到你的.eslintrc.js文件中。
{
  "parser": "@babel/eslint-parser",
  "parserOptions": {
    "babelOptions": {
      "configFile": "/absolute/path/to/babel.config.js"
    }
  },
  ...
}

-1
在你的终端运行 npm install 命令。然后,用以下内容替换你的 babel.config.js 文件的内容:
module.exports = {
  "presets": [
    "@vue/cli-plugin-babel/preset"
  ]
}

这个命令真的完整吗?我错过了开括号 { - Ndrslmpk
module.exports = { //你的代码在这里 } - rabbar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接