firebase-tools icon indicating copy to clipboard operation
firebase-tools copied to clipboard

Support ES Modules for Node 14 functions

Open mbleigh opened this issue 5 years ago • 44 comments

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!

mbleigh avatar Jan 05 '21 17:01 mbleigh

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 avatar Feb 09 '21 21:02 taeold

@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.

mbleigh avatar Feb 09 '21 22:02 mbleigh

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!!

mesqueeb avatar Mar 19 '21 06:03 mesqueeb

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 avatar May 03 '21 17:05 larssn

@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.

mbleigh avatar May 03 '21 18:05 mbleigh

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!

spicemix avatar May 13 '21 23:05 spicemix

@mbleigh Will top level await work if it compiles from TS?

Spencer-Easton avatar May 14 '21 14:05 Spencer-Easton

@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 avatar Jun 28 '21 11:06 taeold

@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 avatar Jun 29 '21 11:06 jthegedus

@jthegedus Good idea. We will probably want to update the template for both JS and TS to reflect ES module support in GCF.

taeold avatar Jun 29 '21 12:06 taeold

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 avatar Jul 01 '21 17:07 DibyodyutiMondal

@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
...

taeold avatar Jul 02 '21 00:07 taeold

Does this aforementioned release include support in the emulator as well?

DibyodyutiMondal avatar Jul 03 '21 22:07 DibyodyutiMondal

@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).

taeold avatar Jul 03 '21 23:07 taeold

@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?

paulvanj avatar Jul 12 '21 21:07 paulvanj

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:

  1. You must select nodejs14 runtime.
  2. You must manually include latest version of @google-cloud/functions-framework dependency.

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.

taeold avatar Jul 12 '21 23:07 taeold

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.)

DibyodyutiMondal avatar Jul 13 '21 10:07 DibyodyutiMondal

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.)

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.

paulvanj avatar Jul 13 '21 10:07 paulvanj

Thanks for filing the issue @paulvanj . Let's carry the discussion on that issue.

taeold avatar Jul 13 '21 12:07 taeold

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:

  1. I have a large code base in the old style, and it would be a moderate pain to migrate it to the new style
  2. I'm using TypeScript, and using node12 (ESM with standard loader) modules means giving up on resolveJsonModule (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.

carlpaten avatar Nov 11 '21 01:11 carlpaten