You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

安装Babel依赖并按官方文档配置后仍出现SyntaxError: Unexpected keyword 'const'错误求助

Fixing Babel's Unexpected keyword 'const' Parse Error

Let's break down why you're hitting this BABEL_PARSE_ERROR and how to fix it step by step:

1. Clean Up Conflicting Dependencies

First, I notice you have both @babel/preset-react (v7) and the outdated babel-preset-react (v6) in your dependencies. These two versions will conflict and cause Babel to misbehave. Uninstall the old one first:

npm uninstall babel-preset-react

2. Fix Your Babel Config

Your current babel.config.json only includes @babel/preset-env, but since you have React-related dependencies, you need to add the official v7 React preset. Even if you aren't using React syntax, explicitly targeting your Node environment will ensure Babel transpiles correctly:

{
  "presets": [
    ["@babel/preset-env", {
      "targets": {
        "node": "current" // Tells Babel to adapt to your active Node version
      }
    }],
    "@babel/preset-react"
  ]
}

3. Ensure You're Running Code Through Babel

The most common mistake here is running code directly with node instead of babel-node—Babel won't process your files unless you explicitly use it to execute them.

  • For one-off test runs:
npx babel-node server.js
  • For development with nodemon (add this to your package.json scripts section):
"scripts": {
  "dev": "nodemon --exec babel-node server.js"
}

Then start your app with npm run dev—this ensures nodemon uses Babel to transpile your code every time it reloads.

4. Reset Dependencies (Just in Case)

Occasionally, cached modules or corrupted installs cause odd issues. Wipe and reinstall your dependencies to rule this out:

rm -rf node_modules package-lock.json
npm install

The core issue here is that Babel wasn't properly processing your code—either due to conflicting presets, an incomplete config, or using the wrong command to start your app. Following these steps should resolve the const parsing error.

内容的提问来源于stack exchange,提问作者Hyejung

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 10:07:28