Babel compilation fails in gulp after reinstalling node_modules but works with CLI

I was using babel with gulp to transform my JavaScript files and everything worked fine. But after I deleted my node_modules folder and ran npm install again, my build process started failing.

Now I get this error:

SyntaxError: main.js: Unexpected token (234:15)

The error shows this line causing problems:

  233 |     options,
> 234 |     isAsync = true,
      |            ^
  235 |     cacheType = 'default',
  236 |     requestMethod = 'POST',
  237 |     requestHeaders = {},

What’s weird is that when I run babel main.js directly in the terminal, it compiles without any issues and produces the expected output.

Here’s my gulp setup:

const gulp = require('gulp');
const { babel } = require('gulp-load-plugins')();

gulp.task('compile', () => {
    return gulp.src([
        'src/main.js'
    ]).pipe(babel()).pipe(gulp.dest('dist'));
});

And my babel config:

{
    "presets": ["es2016"],
    "plugins": [
        "transform-es2015-arrow-functions",
        "transform-es2015-object-super",
        "transform-es2015-parameters",
        "transform-object-assign",
        "transform-es2015-block-scoping",
        "transform-es2015-shorthand-properties",
        "transform-es2015-block-scoped-functions",
        "transform-es2015-for-of",
        "transform-es2015-destructuring",
        ["transform-es2015-classes", {"loose": true}],
        ["transform-es2015-spread", {"loose": true}],
        ["transform-es2015-template-literals", {"loose": true}]
    ]
}

Why would gulp-babel behave differently than the CLI version after reinstalling dependencies?

Check if gulp-load-plugins is loading the right babel package. After reinstalling node_modules, it might grab a different version. Replace const { babel } = require('gulp-load-plugins')(); with const babel = require('gulp-babel'); to use gulp-babel directly. Make sure gulp-babel is installed alongside babel-cli. The syntax error suggests gulp-babel is using an older parser that can’t handle default parameter syntax, while your CLI babel setup works fine. This happens when gulp-load-plugins loads the wrong babel package after reinstalling.

This usually happens when gulp-babel and babel-cli use different configs or plugin versions. After you reinstall node_modules, gulp-babel might not find your babel config file. Try passing the config directly to the babel pipe in your gulp task instead of using the external config file. Also check if gulp-babel’s looking for a different config file name or location. Could be that gulp-babel had some old cached config that got wiped during reinstall. I had the same issue - adding babel options directly in the gulp task fixed the inconsistency between CLI and gulp.

i think gulp-babel may be using a diffrent version of babel than the cli. that isAsync = true might be causing issues since gulp-babel might not support default params. check ur versions or update gulp-babel to resolve this.