Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add Babel support for nullishCoalescingOperator to vue project?

In my Vue-CLI project, when I tried using the ?? operator, I got this error:

Syntax Error: SyntaxError: /Users/stevebennett/odev/freelancing/v-map/src/components/Map.vue: >Support for the experimental syntax 'nullishCoalescingOperator' isn't currently enabled (30:29):

...

Add @babel/plugin-proposal-nullish-coalescing-operator (https://git.io/vb4Se) to the 'plugins' section of your Babel config to enable transformation.

I installed @babel/plugin-syntax-nullish-coalescing-operator (its name seems to have changed), added it to my babel.config.js:

module.exports = {
    presets: ['@vue/app'],
    plugins: ['@babel/plugin-syntax-nullish-coalescing-operator'],
};

Now the error message seems to have gone backwards, no reference to the operator name at all:

Module parse failed: Unexpected token (39:35)
You may need an appropriate loader to handle this file type.
|             case 8:
|               points = _context.sent;
               console.log(sheetID ?? 37);

What am I doing wrong?

like image 834
Steve Bennett Avatar asked Mar 24 '20 01:03

Steve Bennett


People also ask

Does Vue CLI use Babel?

A default Vue CLI project uses @vue/babel-preset-app, which uses @babel/preset-env and the browserslist config to determine the Polyfills needed for your project.

How do I install Babel plugins?

Using a PluginIf the plugin is on npm, you can pass in the name of the plugin and Babel will check that it's installed in node_modules . This is added to the plugins config option, which takes an array. You can also specify an relative/absolute path to your plugin.

What is Babel config js in Vue?

vue. config. js configures Vue. These are two different things. Babel transforms newer Javascript into old Javascript so that older browsers (notably IE11) can understand it.


1 Answers

For me, the @babel/plugin-syntax-nullish-coalescing-operator plugin would not work, which is the one you are using.

I had to use the @babel/plugin-proposal-nullish-coalescing-operator plugin which is the one that the error message suggests you use.

Additionally, I noticed this on the page for the @babel/plugin-syntax-nullish-coalescing-operator plugin:

enter image description here

I can't say for sure if this will fix your problem, but it certainly fixed mine

like image 179
Fearnbuster Avatar answered Oct 23 '22 22:10

Fearnbuster