Support ES Modules for Node 14 functions
Currently, it is not possible to use native ES Modules with Cloud Functions for Firebase in Node 14 due to how triggers are parsed from the code. Unfortunately, adding support is not trivial (or I would have done it when I added Node 14 support generally), but we'd like to do it in the future.
Please upvote this issue if it's important to you!
In addition to problems with how triggers are parsed in firebase-tools repo, the functions framework (used to load and run user-provided code in google cloud functions) doesn't seem to support ESM yet: https://github.com/GoogleCloudPlatform/functions-framework-nodejs/issues/233.
@taeold good catch. Seems like even if we finished the work here we'd be blocked on that. Perhaps we should put a pin in this until work has been done in the functions framework.
I just want to mention to all who come here to upvote:
Don't forget to click "subscribe" on the right side to get updates!!
Does this affect typescript projects?
We use this tsconfig for node 14: https://github.com/tsconfig/bases/blob/master/bases/node14.json
I'm guessing it doesn't.
@larssn correct, since TypeScript with default configuration compiles down into CommonJS, it is not affected -- you can continue to use ESM in TypeScript just like you can in Node < 14.
Although even in a Typescript project, the emulators refuse to use p-queue@7 for instance, leaving me using an old non-ESM version. Thanks in advance!
@mbleigh Will top level await work if it compiles from TS?
@Spencer-Easton Yes. As long the compiled JS code that's deployed to Firebase Functions doesn't contain top level await, things will work.
@taeold
@Spencer-Easton Yes. As long the compiled JS code that's deployed to Firebase Functions doesn't contain top level await, things will work.
Do the ESLint rules generated by firebase init functions capture top-level await? If not, can we configure this rule in the template?
@jthegedus Good idea. We will probably want to update the template for both JS and TS to reflect ES module support in GCF.
So if we manually add in the latest version of functions framework as well as "type":"modules" into our package.json right now, will it work?
@DibyodyutiMondal Not quite. We'll need to make a new release to Firebase CLI that bring support for deploying Firebase Functions using ES modules.
I will mark this issue fixed once we make the release (should be done in next couple of days).
Here's a quick sample for those interested:
// index.js
import functions from "firebase-functions";
export const helloWorld = functions.https.onRequest((request, response) => {
response.send("Hello from Firebase!");
});
// package.json
...
"type": "module",
"dependencies": {
"@google-cloud/functions-framework": "^1.9.0", // Explicitly include functions-framework v1.9.0
...
Does this aforementioned release include support in the emulator as well?
@DibyodyutiMondal Yes. It should work as long as the local version of node used to run the emulator supports ES modules (think that's v13 and up).
@DibyodyutiMondal Not quite. We'll need to make a new release to Firebase CLI that bring support for deploying Firebase Functions using ES modules.
I will mark this issue fixed once we make the release (should be done in next couple of days).
Here's a quick sample for those interested:
// index.js import functions from "firebase-functions"; export const helloWorld = functions.https.onRequest((request, response) => { response.send("Hello from Firebase!"); });// package.json ... "type": "module", "dependencies": { "@google-cloud/functions-framework": "^1.9.0", // Explicitly include functions-framework v1.9.0 ...
Hi guys, any progress with this?
ES module is now supported with the latest firebase-cli version:
$ npm install -g firebase-tools # Get the latest firebase-cli
$ firebase deploy --only functions # Deploy as usual
Note that:
- You must select
nodejs14runtime. - You must manually include latest version of
@google-cloud/functions-frameworkdependency.
e.g.
// package.json
...
"engines": {
"node": "14"
},
"type": "module",
"dependencies": {
"@google-cloud/functions-framework": "^1.9.0",
...
},
If you have any problems with deploying your function packaged as an ES module, please consider reporting a new github issue or filing a support ticket.
I am getting the following error:
i emulators: Starting emulators: auth, functions, firestore, hosting, storage
! functions: The following emulators are not running, calls to these services from the Functions emulator will affect production: database, pubsub
+ functions: Using node@14 from host.
i firestore: Firestore Emulator logging to firestore-debug.log
i hosting: Serving hosting files from: dist/apps/public-client
+ hosting: Local server: http://localhost:5005
i ui: Emulator UI logging to ui-debug.log
i functions: Watching "D:\Github\ros-firebase\dist\libs\cloud-functions" for Cloud Functions...
! functions: Only file and data URLs are supported by the default ESM loader. On Windows, absolute paths must be valid file:// URLs. Received protocol 'd:'
! Your function was killed because it raised an unhandled error.
Is there anything I can do from my side to solve this problem, or am I forced to use commonjs as long as I am developing on windows? (I do not like the idea of adding simport throughout the entire dependency chain of my cloud-functions code.)
I am getting the following error:
i emulators: Starting emulators: auth, functions, firestore, hosting, storage ! functions: The following emulators are not running, calls to these services from the Functions emulator will affect production: database, pubsub + functions: Using node@14 from host. i firestore: Firestore Emulator logging to firestore-debug.log i hosting: Serving hosting files from: dist/apps/public-client + hosting: Local server: http://localhost:5005 i ui: Emulator UI logging to ui-debug.log i functions: Watching "D:\Github\ros-firebase\dist\libs\cloud-functions" for Cloud Functions... ! functions: Only file and data URLs are supported by the default ESM loader. On Windows, absolute paths must be valid file:// URLs. Received protocol 'd:' ! Your function was killed because it raised an unhandled error.Is there anything I can do from my side to solve this problem, or am I forced to use commonjs as long as I am developing on windows? (I do not like the idea of adding
simportthroughout the entire dependency chain of my cloud-functions code.)
getting the same issue. Appears we need to use the type 'module' for the emulator to start up the functions as using 'commonjs' leads to many other issues. advice would be greatly appreciated to resolve this issue mentioned by DibyodyutiMondal.
Thanks for filing the issue @paulvanj . Let's carry the discussion on that issue.
I'm trying to migrate from CommonJS to ES Modules and I'm encountering a difficulty in that the loader algorithm changed from CJS to ESM. In particular, you can't use import red from 'color/red' as shorthand for import red from 'color/red/index.js anymore.
This poses two problems for me:
- I have a large code base in the old style, and it would be a moderate pain to migrate it to the new style
- I'm using TypeScript, and using
node12(ESM with standard loader) modules means giving up onresolveJsonModule(importing JSON files as if they were TypeScript)
Node.js supports customizing the loader via the --experimental-specifier-resolution flag. If I could pass --experimental-specifier-resolution=node to node then I could upgrade painlessly to ES modules, otherwise I'm going to suffer.
I'm guessing I'm not the only one this problem.