Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: C:\Node\gulpfile.js #2417

Closed
DarkLite1 opened this issue Feb 14, 2020 · 6 comments

Comments

@DarkLite1
Copy link

DarkLite1 commented Feb 14, 2020

I'm sorry if this is not the right place to ask this question. But I already tried StackOverflow {1} {2} and other GitHub channels without success. We're trying to move to the new standard using ES6 import and export instead of require.

We're running node v13.6.0 with the follwoing gulpfile.js:

import { src, dest, series, parallel, watch } from 'gulp'
import nodemon from 'gulp-nodemon' // normal nodemon does not display an error on app crash
import env from 'gulp-env'
import browser from 'browser-sync'
import sass from 'gulp-sass'
// Tasks
export default {
    cssTranspile: cssTranspile,
    jsTranspile: jsTranspile,
    server: series(startNodemon, startBrowserSync),
    default: series(
        parallel(
            cssTranspile,
            jsTranspile
        ),
        startNodemon,
        startBrowserSync,
        function () {
            watch('public/scss/*.scss', cssTranspile)
        }
    )
}

The error reported when simply executing gulp in the terminal is:

internal/modules/cjs/loader.js:1160
      throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath);
      ^

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: C:\Node\gulpfile.js
require() of ES modules is not supported.

I must be doing something wrong. Anyone an idea on what it might be?

CLI version: 2.2.0
Local version: 4.0.2

The flag "type": "module", is set in package.json as described in the note docs. Similar issues can be found here and here. Even when trying this example it fails.

Any help on resolving this would be highly appreciated.

@DarkLite1
Copy link
Author

DarkLite1 commented Feb 14, 2020

I found here that you can also name the file gulpfile.esm.js. Then I tried to install the esm pacakge and ran gulp with the following error:

gulp
[09:52:45] Requiring external module esm
C:\Node\gulpfile.esm.js:1
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: C:\Node\gulpfile.esm.js
require() of ES modules is not supported.
require() of C:\Node\gulpfile.esm.js from C:\Users\bob\AppData\Roaming\nvm\v13.8.0\node_modules\gulp-cli\lib\versioned\^4.0.0\index.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename gulpfile.esm.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from C:\Node\package.json.

@phated
Copy link
Member

phated commented Feb 14, 2020

We don't support node's mjs implementation because it requires a complete re-architecture of the project.

We are looking into it, but I wouldn't expect it to arrive soon.

As for using esm, the last line tells you how to fix it.

remove "type": "module" from C:\Node\package.json

@phated phated closed this as completed Feb 14, 2020
@DarkLite1
Copy link
Author

Thank you for the feedback. In one of the options it also says:

Instead rename gulpfile.esm.js to end in .cjs,

So when I rename gulpfile.esm.js to gulfile.esm.cjs it's still not working. I'm a bit reluctant to remove the "type": "module" form the package.json. As it is described in the node.js docs that this is best practice.

@phated
Copy link
Member

phated commented Feb 14, 2020

We don't support .cjs extension because it was determined that was a bad practice. We create a custom hook to support .esm.js.

@DarkLite1
Copy link
Author

DarkLite1 commented Feb 14, 2020

If the .cjs extension is not supported it should not be suggested as an option. That aside, if .ems.js is supported how should I get both to work together? Without removing the "type": "module" option from the package.json?

@phated
Copy link
Member

phated commented Feb 14, 2020

It's being suggested by the esm library, which we do not maintain.

That aside, if .ems.js is supported how should I get both to work together? Without removing the "type": "module" option from the package.json?

You don't. I'm going to lock this as it's not a conversation.

@gulpjs gulpjs locked and limited conversation to collaborators Feb 14, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants