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 thrown when due to failure to use import() for es6 modules with node v14 (beta) #2985

Closed
joelclimbsthings opened this issue Dec 31, 2020 · 4 comments

Comments

@joelclimbsthings
Copy link

[REQUIRED] Environment info

firebase-tools: 9.10.0

Platform: Ubuntu

[REQUIRED] Test case

Provided a small PoC repo here: https://github.com/joelclimbsthings/firebase-tools-es6-modules-issue

Basically, it's just a basic project with the "engine" set to 14 and trying to deploy.

[REQUIRED] Steps to reproduce

Check out this repo: https://github.com/joelclimbsthings/firebase-tools-es6-modules-issue

And run the deploy script against any valid project. The index file is intentionally empty since the error is thrown before that matters.

[REQUIRED] Expected behavior

The main index.js file is successfully imported as an es6 module.

[REQUIRED] Actual behavior

This error is thrown when attempting to deploy:

Error: Error occurred while parsing your function triggers.

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /home/joelt/project/firebase-tools-issue/index.js
require() of ES modules is not supported.
require() of /home/joelt/project/firebase-tools-issue/index.js from /home/joelt/project/firebase-tools-issue/node_modules/firebase-tools/lib/triggerParser.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 index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /home/joelt/project/firebase-tools-issue/package.json.

    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1080:13)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Module.require (internal/modules/cjs/loader.js:952:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at /home/joelt/project/firebase-tools-issue/node_modules/firebase-tools/lib/triggerParser.js:15:15
    at Object.<anonymous> (/home/joelt/project/firebase-tools-issue/node_modules/firebase-tools/lib/triggerParser.js:53:3)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)

It looks like the error originates from this line:

mod = require(packageDir);

And changing the require() to an import() would seem to remedy the issue (or at least the error).

Is there any kind of workaround for this? I'm just experiementing with using the beta Node v14 engine to support es6 module syntax.

@samtstern
Copy link
Contributor

samtstern commented Dec 31, 2020

@joelclimbsthings thanks for the clear reproduction! I was able to reproduce your issue and I noticed that it only happens when "type": "module" is declared in package.json.

@samtstern
Copy link
Contributor

@mbleigh @bkendall @abeisgoat any idea what we can do about this? I tried to experiment with reading the type field of the package.json and then using import() instead of require() but because our tsconfig.json specifies module: "commonjs" the import call gets compiled down to a require call by TypeScript.

Is the answer just that we don't support ES6 modules?

@joelclimbsthings
Copy link
Author

I was able to reproduce your issue and I noticed that it only happens when "type": "module" is declared in package.json.

Thanks for the prompt response, @samtstern ! Correct, the main reason I was giving v14 a spin with my functions was the support of es6 modules, and those are only supported within Node.js when type: module is declared. My main reason for giving v14 a spin was these modules, so that's basically my question as well; are they meant to be supported?

Thanks

@mbleigh
Copy link
Contributor

mbleigh commented Jan 5, 2021

We don't yet support ES Modules in Node 14 -- I've created a canonical feature request to do so and will close this as a duplicate of #2994.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants