Cannot use import statement outside a module - Timber Starter Theme - Gulp

Viewed 2921

I'm coding a simple website using Timber Starter Theme on Wordpress but I need import some modules in my js files. I use gulp an here is my gulp file

// Gulp (https://www.npmjs.com/package/gulp)
const gulp        = require('gulp')
// Compile scss to css (https://www.npmjs.com/package/gulp-sass)
const sass        = require('gulp-sass')
// Rename file (https://www.npmjs.com/package/gulp-rename)
const rename      = require('gulp-rename')
// Minify JS with UglifyJS3 (https://www.npmjs.com/package/gulp-uglify)
const uglify      = require('gulp-uglify')
// Minify CSS (https://www.npmjs.com/package/gulp-clean-css)
const cleancss    = require('gulp-clean-css')
// Concat all file, in one (https://www.npmjs.com/package/gulp-concat)
const concat      = require('gulp-concat')
// Images size more small (https://www.npmjs.com/package/gulp-imagemin)
const imagemin    = require('gulp-imagemin')
// Notification on your Mac/PC (https://www.npmjs.com/package/gulp-notify)
const notify      = require('gulp-notify')
// Write ES6 > compile to ES5 (https://www.npmjs.com/package/gulp-babel)
//const babel     = require('gulp-babel')
var babel = require('@rollup/plugin-babel');

// Browser Sync (for live render -  (https://www.npmjs.com/package/browser-sync))
const browsersync = require('browser-sync').create()



// SourceMaps, add path impacted file (https://www.npmjs.com/package/gulp-sourcemaps)
const sourcemaps  = require('gulp-sourcemaps')

// Stop the process if an error is thrown.
const plumber = require("gulp-plumber")





// PATH
const paths = {
  js: {
    src: ['./js/**/*.js', './js/*.js'],
    dest: './dist/js'
  },
  css: {
      src: ['./scss/**/*.scss', './scss/*.scss'],
      dest: './css'
  },
  html: {
      src: ['./templates/*.twig', './templates/**/*.twig' ]
  }
}

const styles = () =>
    gulp.src(paths.css.src)
        .pipe(sourcemaps.init({loadMaps: true}))
        .pipe(sass().on('error', sass.logError))
        .pipe(cleancss())
        .pipe(concat('main.css'))
        .pipe(rename({ suffix: ".min" }))
        .pipe(sourcemaps.write('./'))
        .pipe(gulp.dest(paths.css.dest))
        .pipe(browsersync.stream())
        //.pipe(notify("Css ok !"))// JS

      const scripts = () =>
    gulp.src(paths.js.src)
        .pipe(plumber())
        // Transpile the JS code using Babel's preset-env.
        .pipe(
          babel({
            presets: [
              [
                "@babel/env",
                {
                  modules: true
                }
              ]
            ]
          })
        )
        .pipe(gulp.dest(paths.js.dest))
        //.pipe(browsersync.stream())
        //.pipe(notify("Js ok !"))


    const browserSyncWatch = () => {
      browsersync.init({
          proxy: "maeva.live",
          // ou proxy: "yourlocal.dev",
          port: 3000
      })
    }

    const twig = () =>
    gulp.src(paths.html.src)
        .pipe(browsersync.reload({ stream: true }));

    

    const watchFiles = () =>
    gulp.watch(paths.js.src, scripts)
    gulp.watch(paths.css.src, styles)
    gulp.watch(paths.html.src, twig);



    const watcher = gulp.parallel(watchFiles, browserSyncWatch)
    const build = gulp.series(gulp.parallel(styles));

    exports.watch = watcher         // exec with :  gulp watch

I've an index.js where i tried to do simply

import 'gsap';

and I obtained the next error message : Uncaught SyntaxError: Cannot use import statement outside a module

My js file is loaded in ma functions.php file

      wp_enqueue_script( 'entry-js', get_template_directory_uri() . '/js/index.js', array( 'jquery' ), '1.0', true);

When I comment this line, nothing appear in my console. I don't really understand how gulp works to enqueue script. I'm a pretty lost...

Thanks !

1 Answers

SyntaxError: Cannot use import statement outside a module could be solved by one of these options:

  1. Add the following to your package.json file

    {
       "type": "module"
    }
    
  2. Rename your JS files using the .mjs extension

  3. Consider using Parcel instead of Gulp. CSS/JS implicitly builds swithout the configuration complexity and natively handles import with ES Module import syntax. See this Parcel example (you would specify your index.php file instead of the html file.

Related