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

ESM modules not supported per Nodejs v14 docs #850

Closed
jimmont opened this issue Jan 23, 2021 · 10 comments
Closed

ESM modules not supported per Nodejs v14 docs #850

jimmont opened this issue Jan 23, 2021 · 10 comments

Comments

@jimmont
Copy link

jimmont commented Jan 23, 2021

Related issues

I don't know.

[REQUIRED] Version info

node -v
v14.15.4
firebase -V
9.2.2

firebase-functions:

"firebase-functions": "^3.13.1"

firebase-tools:

firebase -V
9.2.2
> npm ls -g --depth=
...
├── firebase@8.2.4
├── firebase-tools@9.2.2

firebase-admin:

"firebase-admin": "^9.4.2",

[REQUIRED] Test case

No.

[REQUIRED] Steps to reproduce

// functions/package.json
{
  "type": "module",
  "engines": {
    "node": "14"
  },
  "main": "index.js",
  "dependencies": {
    "firebase-admin": "^9.4.2",
    "firebase-functions": "^3.13.1"
  },
  "private": true
}

// functions/index.js
import functions from 'firebase-functions';
console.log(typeof functions);

// run it
 firebase emulators:start --only functions

[REQUIRED] Expected behavior

stdout shows object

[REQUIRED] Actual behavior

> firebase emulators:start --only functions
i  emulators: Starting emulators: functions
⚠  functions: The following emulators are not running, calls to these services from the Functions emulator will affect production: auth, firestore, database, hosting, pubsub
✔  functions: Using node@14 from host.
i  ui: Emulator UI logging to ui-debug.log
i  functions: Watching "/redacted/functions" for Cloud Functions...
⚠  Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /redacted/functions/index.js
require() of ES modules is not supported.
require() of /redacted/functions/index.js from /usr/local/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.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 /redacted/functions/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 initializeRuntime (/usr/local/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:676:29)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at async handleMessage (/usr/local/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:714:20)
⚠  We were unable to load your functions code. (see above)

Were you able to successfully deploy your functions?

Didn't try.

Same behavior in Nodejs v14 and v15 (this report from supported v14) and using .mjs extension instead of .js also with adjustment to main to match.
see also https://nodejs.org/docs/latest-v14.x/api/esm.html

@google-oss-bot
Copy link
Collaborator

I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.

@samtstern
Copy link
Contributor

See:

Right now we don't support ES Modules but we're tracking it as a feature request.

@jbistis
Copy link

jbistis commented Jan 4, 2022

What is the fix for this error? We are on node v12.20.2, firebase-admin 9.2.0, firebase-functions 3.11.0 and our firebase deploy errors when we try to import a library built with
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"target": "es2015",
"module": "es2015",
"moduleResolution": "node",
"declaration": true,
"declarationMap": true,
"inlineSources": true,
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"importHelpers": true,
"types": [],
"lib": [
"dom",
"es2018"
]
},
"angularCompilerOptions": {
"allowSyntheticDefaultImports": true,
"annotateForClosureCompiler": true,
"skipTemplateCodegen": true,
"strictMetadataEmit": true,
"fullTemplateTypeCheck": true,
"strictInjectionParameters": true,
"enableResourceInlining": true,
"enableIvy": false
},

@taeold
Copy link
Contributor

taeold commented Jan 4, 2022

Hey @jbistis. I don't have a full picture of what's going on with your setting based on the info you shared, but I do notice that you are using node.js 12. IIUC, node.js 12 does not supporrt ES modules - you need at least v13 for that.

@jbistis
Copy link

jbistis commented Jan 4, 2022

When I run firebase deploy --only functions, it fails with:
Error: Error occurred while parsing your function triggers.
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /dev/pdl-nx/functions/lib/index.js require() of ES modules is not supported.

I only get this error when I import models from our new lib. If I import models from our old lib, the firebase deploy command completes just fine. That is why I think there must be some difference in the tsconfig settings in the old lib that I am not setting in the new lib but cannot seem to find it. I have enableIvy set to false in both. Any ideas?

@taeold
Copy link
Contributor

taeold commented Jan 4, 2022

From the error message, my guess is that your new lib packaged as a ES module whereas the old module was a commonjs module? Do you think that's making a difference?

Also, I'd double check that you are using latest version of the Firebase CLI - the esmodule support was added only in the last few months.

@jbistis
Copy link

jbistis commented Jan 4, 2022

Excellent feedback. Thank you. The new lib is built with Angular CLI 13.4.1 and the old lib is Angular CLI 12.2.13. Both are firebase --version 9.2.1. I just tried publishing the a13 lib as a commonjs module using
"compilerOptions": {
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"removeComments": true,
"sourceMap": true,
"outDir": "../lib",
"declaration": true
}
and then tried a firebase deploy --only functions ... still getting Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: I also just to firebase-functions 31.6 and node v14.18.2 and still getting the same error message. Is there a firebase version that can import a lib built with 13.4.1 so that firebase deploy --only functions does not crash with the Error [ERR_REQUIRE_ESM]: Must use import to load ES Module error?

@taeold
Copy link
Contributor

taeold commented Jan 4, 2022

@jbistis

firebase --version 9.2.1.

I think updating the firebase cli might help here, e.g.

$ npm i -g firebasee-tools@latest

I don't know the exact version of the CLI where we introduced the ES module support for functions, but 9.2.1 is definitely too old.

@jbistis
Copy link

jbistis commented Jan 4, 2022

I tried upgrading to both firebase-tools 9.23.3 and the latest version 10.0.1 which came out 19 days ago and the same error.

✔ functions: Finished running predeploy script.
i functions: ensuring required API cloudfunctions.googleapis.com is enabled...
i functions: ensuring required API cloudbuild.googleapis.com is enabled...
✔ functions: required API cloudfunctions.googleapis.com is enabled
✔ functions: required API cloudbuild.googleapis.com is enabled
i functions: preparing functions directory for uploading...

Error: Error occurred while parsing your function triggers.

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: functions/node_modules/@prometheuspartners/types/fesm2015/prometheuspartners-types.mjs

@jbistis
Copy link

jbistis commented Jan 4, 2022

@taeold, I redid the firebase-tools upgrade to 10.0.1 and it all working now. Fighting a different issue now.

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

5 participants