question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add support for mjs gulpfile

See original GitHub issue

Hello,

As of Node 8.5.0, we can now have native support for ESM (https://nodejs.org/en/blog/release/v8.5.0/#header-notable-changes). I know this feature is still experimental, but I think it would be nice if gulp would support it, in the same way it already supports gulpfile.babel.js.

What were you expecting to happen?

$ gulp
[16:10:25] Using gulpfile.mjs - ExperimentalWarning: The ESM module loader is experimental.
[16:10:25] Starting 'default'...
Gulp with ES module! Yeah!
[16:10:25] Finished 'default' after 1.62 ms

What actually happened?

$ gulp
[16:10:19] No gulpfile found
$ gulp --gulpfile gulpfile.mjs
.../gulpfile.mjs:1
(function (exports, require, module, __filename, __dirname) { import gulp from "gulp";
                                                              ^^^^^^

SyntaxError: Unexpected token import
    at createScript (vm.js:74:10)
    at Object.runInThisContext (vm.js:116:10)
    at Module._compile (module.js:588:28)
    at Object.Module._extensions..js (module.js:635:10)
    at Module.load (module.js:545:32)
    at tryModuleLoad (module.js:508:12)
    at Function.Module._load (module.js:500:3)
    at Module.require (module.js:568:17)
    at require (internal/module.js:11:18)
    at execute (/usr/local/lib/node_modules/gulp-cli/lib/versioned/^4.0.0-alpha.2/index.js:35:18)

The gulpfile I used:

import gulp from "gulp";

const testTask = () =>
  new Promise(done => {
    console.log("Gulp with ES module! Yeah!");
    done();
  });

export default testTask;

What version of gulp are you using?

$ gulp -v
[16:42:58] CLI version 1.4.0
[16:42:58] Local version 4.0.0-alpha.2

What versions of npm and node are you using?

$ node -v
v8.5.0
$ npm -v
5.3.0
$ yarn -v
1.0.2

Thank you again for your amazing work at making Gulp!

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:5
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
phatedcommented, Jan 21, 2020

If you want imports right now, it’s easiest to rename your gulpfile with .ts and install typescript + ts-node (no config files necessary).

1reaction
xiaoxiangmoecommented, Jan 20, 2020

@phated can you reopen this issue? Now with Node 13.2 .mjs works without --experimental-modules.

Read more comments on GitHub >

github_iconTop Results From Across the Web

JavaScript and Gulpfiles - gulp.js
For Babel, rename to gulpfile.babel.js and install the @babel/register module. Most new versions of node support most features that TypeScript or Babel ...
Read more >
gulp-mjs - npm
Start using gulp-mjs in your project by running `npm i gulp-mjs`. There is 1 other project in the npm registry using gulp-mjs.
Read more >
Getting ` Error [ERR_REQUIRE_ESM]` while running `gulp ...
I'm getting errors like - Error [ERR_REQUIRE_ESM]: require() of ES Module E:\Projects\portfolio\node_modules\gulp-imagemin\index.js from E:\ ...
Read more >
Convert scripts/Gulpfile to checked mjs/cjs so they can run without ...
and then running npm install . There is also a playground for this build and an npm module you can use via "typescript":...
Read more >
Documentation - ECMAScript Modules in Node.js - TypeScript
Node.js supports two extensions to help with this: .mjs and .cjs . .mjs files are ... for your type declarations, you can add...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found