解决问题:This experimental syntax requires enabling one of the following parser plugins

2022-08-20大约1分钟

使用一些ES6新语法,或者JSX的时候,新项目可能会遇到这样的错:Parsing error: This experimental syntax requires enabling one of the following parser plugin(s): 'jsx, flow, typescript'

问题原因出在缺少Babel的配置文件,在项目根目录下添加.babelrc文件及相关的babel配置即可。

比如这是一个示例配置:

module.exports = {
    presets: [
        [
            '@babel/preset-env',
            {
                targets: {
                    esmodules: true
                }
            }
        ],
        '@babel/preset-react'
    ],
    plugins: [
        [
            '@babel/plugin-proposal-class-properties',
            {
                loose: false
            }
        ]
    ]
};;
上面文件用的preset和plugin需要根据自己的实际情况来填写,直接照搬可能会出问题。