Closed tdkehoe closed 1 year ago
This issue does not have all the information required by the template. Looks like you forgot to fill out some sections. Please update the issue with more information.
It seems you got steered off course very early on in your process. Though firebase deploy
currently builds your functions for you, firebase emulators:start
doesn't. I recommend running tsc watch
in another terminal while you run your emulators so that you get hot-reloading functions code in the emulator.
Do not try to load any typescript code into node directly. It's not how TypeScript works.
Are you saying that the error SyntaxError: Cannot use import statement outside a module
can't be fixed, that I shouldn't set up Firebase Cloud Functions as ES modules (using import
and export
), and that I should go back to using CommonJS modules (with require
and exports
)? This is what I've done and my head has stopped hurting. :-)
Our cloud-functions rely on a separate library package that we obtain via npm locally ("ticore": "file:../ticore"). However, we encounter an issue where the cloud functions fail to run with an "cannot use import statement outside a module" error when we modify the tsconfig file of this library and set the module property value to esnext. Is there any alternative to using the commonjs module that would allow us to avoid this problem?
I ran into this same issue.
In package.json change "main": "lib/index.js", to "main": "src/index.ts"
This is the mistake. I made the same change, probably thinking that the default setup template (using firebase init) was wrong for typescript. It's not. The main is supposed to point to the built js
file, not the source ts
file.
Once you change that back, ES Modules ("target": "ESNext", "module": "ESNext", ...
) work just fine.
[REQUIRED] Environment info
firebase-admin@11.2.0 firebase-tools@11.15.0 firebase@9.12.1 node: 'v18.11.0' npm: '8.19.2'
firebase-tools:
macOS Big Sur 11.7
Platform:
[REQUIRED] Test case
This is the default
package.json
when you spin up Firebase Cloud Functions.[REQUIRED] Steps to reproduce
npm install -g @angular/cli ng new CloudFunctions cd CloudFunctions npm install -g firebase-tools npm audit fix --force firebase init
Select
Emulators
. SelectCreate a new project
. SelectFunctions Emulator
andFirestore Emulator
.firebase login firebase init functions
Select
TypeScript
.firebase emulators:start
Error:
functions/lib/index.js does not exist, can't deploy Cloud Functions
In
package.json
change"main": "lib/index.js",
to"main": "src/index.ts",
.Error:
SyntaxError: Cannot use import statement outside a module
This is the error I can't fix in TypeScript.
First, that error message should say
SyntaxError: Cannot use import statement outside an ES module
. As written the error message suggests that you could useimport
in a CommonJS module, which is debatable.I can fix the error by switching to JavaScript. This suggests that the problem is in
tsconfig.json
, which JavaScript doesn't use.Change
index.ts
toindex.js
. Inpackage.json
change"main": "lib/index.js",
to"main": "src/index.js",
. Add"type": "module",
.Success! To test that the JavaScript Cloud Functions are working with ES modules, add this cloud function to
index.js
:Open your browser to
http://127.0.0.1:4000/functions
. In theFirestore
tab, start a collectionmessages
. In the document make a fieldoriginal
with a string valuehello world
. In a moment you should see `uppercase: "HELLO WORLD".Let's switch back to TypeScript. Change
index.js
toindex.ts
. Inpackage.json
changesrc/index.js
tosrc/index.ts
.This throws an error:
` `` functions/src/index.ts does not exist, can't deploy Cloud Functions
SyntaxError: Cannot use import statement outside a module
In
tsconfig.json
:Change
"module": "commonjs",
to "module": "ESNext",. It would be better to leave this with
commonJSas you can then import both
commonJSand
ES` modules.Change
"target": "es2017"
to"target": "ESNext"
. This updates to the latest ECMAScript.Switch
strict
tofalse
.Change
outDir
tosrc
.My
tsconfig.json
now looks like this:It's still throwing the error
I believe there's something else misconfigured in
tsconfig.json
because if we switch back to JavaScript the code runs.[REQUIRED] Expected behavior
When you add a document with a field
original
in the Firestore emulator, a few minutes later a fielduppercase
is added to the document.[REQUIRED] Actual behavior
Throws error
SyntaxError: Cannot use import statement outside a module
firebase emulators:start --debug
[2022-10-26T16:08:19.482Z] > command requires scopes: ["email","openid","https://www.googleapis.com/auth/cloudplatformprojects.readonly","https://www.googleapis.com/auth/firebase","https://www.googleapis.com/auth/cloud-platform"] [2022-10-26T16:08:19.483Z] > authorizing via signed-in user (thomasdkehoe@gmail.com) [2022-10-26T16:08:19.580Z] java version "19" 2022-09-20
[2022-10-26T16:08:19.581Z] Java(TM) SE Runtime Environment (build 19+36-2238) Java HotSpot(TM) 64-Bit Server VM (build 19+36-2238, mixed mode, sharing)
[2022-10-26T16:08:19.586Z] Parsed Java major version: 19 i emulators: Starting emulators: functions, firestore {"metadata":{"emulator":{"name":"hub"},"message":"Starting emulators: functions, firestore"}} [2022-10-26T16:08:20.263Z] [logging] Logging Emulator only supports listening on one address (127.0.0.1). Not listening on ::1 [2022-10-26T16:08:20.264Z] [firestore] Firestore Emulator only supports listening on one address (127.0.0.1). Not listening on ::1 [2022-10-26T16:08:20.264Z] [firestore.websocket] websocket server for firestore only supports listening on one address (127.0.0.1). Not listening on ::1 [2022-10-26T16:08:20.264Z] assigned listening specs for emulators {"user":{"hub":[{"address":"127.0.0.1","family":"IPv4","port":4400},{"address":"::1","family":"IPv6","port":4400}],"ui":[{"address":"127.0.0.1","family":"IPv4","port":4000},{"address":"::1","family":"IPv6","port":4000}],"logging":[{"address":"127.0.0.1","family":"IPv4","port":4500}],"firestore":[{"address":"127.0.0.1","family":"IPv4","port":8080}],"firestore.websocket":[{"address":"127.0.0.1","family":"IPv4","port":9150}]},"metadata":{"message":"assigned listening specs for emulators"}} [2022-10-26T16:08:20.275Z] [hub] writing locator at /var/folders/_r/0dhg_r457r97_vy_3y7k2_9r0000gn/T/hub-triggerable-functions-project.json [2022-10-26T16:08:20.547Z] [functions] Functions Emulator only supports listening on one address (127.0.0.1). Not listening on ::1 [2022-10-26T16:08:20.547Z] [eventarc] Eventarc Emulator only supports listening on one address (127.0.0.1). Not listening on ::1 [2022-10-26T16:08:20.547Z] late-assigned ports for functions and eventarc emulators {"user":{"hub":[{"address":"127.0.0.1","family":"IPv4","port":4400},{"address":"::1","family":"IPv6","port":4400}],"ui":[{"address":"127.0.0.1","family":"IPv4","port":4000},{"address":"::1","family":"IPv6","port":4000}],"logging":[{"address":"127.0.0.1","family":"IPv4","port":4500}],"firestore":[{"address":"127.0.0.1","family":"IPv4","port":8080}],"firestore.websocket":[{"address":"127.0.0.1","family":"IPv4","port":9150}],"functions":[{"address":"127.0.0.1","family":"IPv4","port":5001}],"eventarc":[{"address":"127.0.0.1","family":"IPv4","port":9299}]},"metadata":{"message":"late-assigned ports for functions and eventarc emulators"}} ⚠ functions: The following emulators are not running, calls to these services from the Functions emulator will affect production: auth, database, hosting, pubsub, storage {"metadata":{"emulator":{"name":"functions"},"message":"The following emulators are not running, calls to these services from the Functions emulator will affect production: \u001b[1mauth, database, hosting, pubsub, storage\u001b[22m"}} ⚠ Your requested "node" version "16" doesn't match your global version "18". Using node@18 from host. {"metadata":{"emulator":{"name":"functions"},"message":"Your requested \"node\" version \"16\" doesn't match your global version \"18\". Using node@18 from host."}} [2022-10-26T16:08:20.554Z] defaultcredentials: writing to file /Users/TDK/.config/firebase/thomasdkehoe_gmail_com_application_default_credentials.json [2022-10-26T16:08:20.557Z] Setting GAC to /Users/TDK/.config/firebase/thomasdkehoe_gmail_com_application_default_credentials.json {"metadata":{"emulator":{"name":"functions"},"message":"Setting GAC to /Users/TDK/.config/firebase/thomasdkehoe_gmail_com_application_default_credentials.json"}} [2022-10-26T16:08:20.558Z] > refreshing access token with scopes: [] [2022-10-26T16:08:20.560Z] >>> [apiv2][query] POST https://www.googleapis.com/oauth2/v3/token [none] [2022-10-26T16:08:20.560Z] >>> [apiv2][body] POST https://www.googleapis.com/oauth2/v3/token [omitted] [2022-10-26T16:08:20.637Z] <<< [apiv2][status] POST https://www.googleapis.com/oauth2/v3/token 200 [2022-10-26T16:08:20.637Z] <<< [apiv2][body] POST https://www.googleapis.com/oauth2/v3/token [omitted] [2022-10-26T16:08:20.656Z] >>> [apiv2][query] GET https://firebase.googleapis.com/v1beta1/projects/triggerable-functions-project/adminSdkConfig [none] [2022-10-26T16:08:20.812Z] <<< [apiv2][status] GET https://firebase.googleapis.com/v1beta1/projects/triggerable-functions-project/adminSdkConfig 200 [2022-10-26T16:08:20.812Z] <<< [apiv2][body] GET https://firebase.googleapis.com/v1beta1/projects/triggerable-functions-project/adminSdkConfig {"projectId":"triggerable-functions-project","storageBucket":"triggerable-functions-project.appspot.com","locationId":"us-central"} ⚠ firestore: Did not find a Cloud Firestore rules file specified in a firebase.json config file. {"metadata":{"emulator":{"name":"firestore"},"message":"Did not find a Cloud Firestore rules file specified in a firebase.json config file."}} ⚠ firestore: The emulator will default to allowing all reads and writes. Learn more about this option: https://firebase.google.com/docs/emulator-suite/install_and_configure#security_rules_configuration. {"metadata":{"emulator":{"name":"firestore"},"message":"The emulator will default to allowing all reads and writes. Learn more about this option: https://firebase.google.com/docs/emulator-suite/install_and_configure#security_rules_configuration."}} [2022-10-26T16:08:20.834Z] Ignoring unsupported arg: auto_download {"metadata":{"emulator":{"name":"firestore"},"message":"Ignoring unsupported arg: auto_download"}} [2022-10-26T16:08:20.834Z] Ignoring unsupported arg: single_project_mode_error {"metadata":{"emulator":{"name":"firestore"},"message":"Ignoring unsupported arg: single_project_mode_error"}} [2022-10-26T16:08:20.834Z] Starting Firestore Emulator with command {"binary":"java","args":["-Dgoogle.cloud_firestore.debug_log_level=FINE","-Duser.language=en","-jar","/Users/TDK/.cache/firebase/emulators/cloud-firestore-emulator-v1.15.1.jar","--host","127.0.0.1","--port",8080,"--websocket_port",9150,"--project_id","triggerable-functions-project","--single_project_mode",true,"--functions_emulator","127.0.0.1:5001"],"optionalArgs":["port","webchannel_port","host","rules","websocket_port","functions_emulator","seed_from_export","project_id","single_project_mode"],"joinArgs":false} {"metadata":{"emulator":{"name":"firestore"},"message":"Starting Firestore Emulator with command {\"binary\":\"java\",\"args\":[\"-Dgoogle.cloud_firestore.debug_log_level=FINE\",\"-Duser.language=en\",\"-jar\",\"/Users/TDK/.cache/firebase/emulators/cloud-firestore-emulator-v1.15.1.jar\",\"--host\",\"127.0.0.1\",\"--port\",8080,\"--websocket_port\",9150,\"--project_id\",\"triggerable-functions-project\",\"--single_project_mode\",true,\"--functions_emulator\",\"127.0.0.1:5001\"],\"optionalArgs\":[\"port\",\"webchannel_port\",\"host\",\"rules\",\"websocket_port\",\"functions_emulator\",\"seed_from_export\",\"project_id\",\"single_project_mode\"],\"joinArgs\":false}"}} i firestore: Firestore Emulator logging to firestore-debug.log {"metadata":{"emulator":{"name":"firestore"},"message":"Firestore Emulator logging to \u001b[1mfirestore-debug.log\u001b[22m"}} [2022-10-26T16:08:22.018Z] Oct 26, 2022 10:08:21 AM com.google.cloud.datastore.emulator.firestore.websocket.WebSocketServer start INFO: Started WebSocket server on ws://127.0.0.1:9150 {"metadata":{"emulator":{"name":"firestore"},"message":"Oct 26, 2022 10:08:21 AM com.google.cloud.datastore.emulator.firestore.websocket.WebSocketServer start\nINFO: Started WebSocket server on ws://127.0.0.1:9150\n"}} [2022-10-26T16:08:22.048Z] API endpoint: http:// {"metadata":{"emulator":{"name":"firestore"},"message":"API endpoint: http://"}} [2022-10-26T16:08:22.048Z] 127.0.0.1:8080 If you are using a library that supports the FIRESTORE_EMULATOR_HOST environment variable, run:
export FIRESTORE_EMULATOR_HOST=127.0.0.1:8080
Dev App Server is now running.
{"metadata":{"emulator":{"name":"firestore"},"message":"127.0.0.1:8080\nIf you are using a library that supports the FIRESTORE_EMULATOR_HOST environment variable, run:\n\n export FIRESTORE_EMULATOR_HOST=127.0.0.1:8080\n\nDev App Server is now running.\n\n"}} ✔ firestore: Firestore Emulator UI websocket is running on 9150. {"metadata":{"emulator":{"name":"firestore"},"message":"Firestore Emulator UI websocket is running on 9150."}} [2022-10-26T16:08:22.116Z] Ignoring unsupported arg: auto_download {"metadata":{"emulator":{"name":"ui"},"message":"Ignoring unsupported arg: auto_download"}} [2022-10-26T16:08:22.117Z] Ignoring unsupported arg: port {"metadata":{"emulator":{"name":"ui"},"message":"Ignoring unsupported arg: port"}} [2022-10-26T16:08:22.117Z] Starting Emulator UI with command {"binary":"node","args":["/Users/TDK/.cache/firebase/emulators/ui-v1.11.1/server/server.js"],"optionalArgs":[],"joinArgs":false} {"metadata":{"emulator":{"name":"ui"},"message":"Starting Emulator UI with command {\"binary\":\"node\",\"args\":[\"/Users/TDK/.cache/firebase/emulators/ui-v1.11.1/server/server.js\"],\"optionalArgs\":[],\"joinArgs\":false}"}} i ui: Emulator UI logging to ui-debug.log {"metadata":{"emulator":{"name":"ui"},"message":"Emulator UI logging to \u001b[1mui-debug.log\u001b[22m"}} [2022-10-26T16:08:22.256Z] Web / API server started at 127.0.0.1:4000 {"metadata":{"emulator":{"name":"ui"},"message":"Web / API server started at 127.0.0.1:4000\n"}} [2022-10-26T16:08:22.256Z] Web / API server started at ::1:4000 {"metadata":{"emulator":{"name":"ui"},"message":"Web / API server started at ::1:4000\n"}} i functions: Watching "/Users/TDK/VisualStudioCodeProjects/CloudFunctions/functions" for Cloud Functions... {"metadata":{"emulator":{"name":"functions"},"message":"Watching \"/Users/TDK/VisualStudioCodeProjects/CloudFunctions/functions\" for Cloud Functions..."}} [2022-10-26T16:08:22.379Z] Validating nodejs source [2022-10-26T16:08:24.474Z] > [functions] package.json contents: { "name": "functions", "type": "module", "scripts": { "lint": "eslint --ext .js,.ts .", "build": "tsc", "build:watch": "tsc --watch", "serve": "npm run build && firebase emulators:start --only functions", "shell": "npm run build && firebase functions:shell", "start": "npm run shell", "deploy": "firebase deploy --only functions", "logs": "firebase functions:log" }, "engines": { "node": "16" }, "main": "src/index.mts", "dependencies": { "firebase-admin": "^11.2.0", "firebase-functions": "^4.0.1" }, "devDependencies": { "@typescript-eslint/eslint-plugin": "^5.12.0", "@typescript-eslint/parser": "^5.12.0", "eslint": "^8.9.0", "eslint-config-google": "^0.14.0", "eslint-plugin-import": "^2.25.4", "firebase-functions-test": "^0.2.0", "typescript": "^4.8.4" }, "private": true } [2022-10-26T16:08:24.475Z] Building nodejs source [2022-10-26T16:08:24.475Z] Analyzing nodejs backend spec [2022-10-26T16:08:24.477Z] Could not find functions.yaml. Must use http discovery [2022-10-26T16:08:24.537Z] Oct 26, 2022 10:08:24 AM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead INFO: Detected non-HTTP/2 connection. {"metadata":{"emulator":{"name":"firestore"},"message":"Oct 26, 2022 10:08:24 AM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead\nINFO: Detected non-HTTP/2 connection.\n"}} [2022-10-26T16:08:24.538Z] Oct 26, 2022 10:08:24 AM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead INFO: Detected non-HTTP/2 connection. {"metadata":{"emulator":{"name":"firestore"},"message":"Oct 26, 2022 10:08:24 AM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead\nINFO: Detected non-HTTP/2 connection.\n"}} [2022-10-26T16:08:24.540Z] Oct 26, 2022 10:08:24 AM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead INFO: Detected non-HTTP/2 connection. {"metadata":{"emulator":{"name":"firestore"},"message":"Oct 26, 2022 10:08:24 AM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead\nINFO: Detected non-HTTP/2 connection.\n"}} [2022-10-26T16:08:24.545Z] Oct 26, 2022 10:08:24 AM com.google.cloud.datastore.emulator.firestore.websocket.WebSocketChannelHandler initChannel INFO: Connected to new websocket client {"metadata":{"emulator":{"name":"firestore"},"message":"Oct 26, 2022 10:08:24 AM com.google.cloud.datastore.emulator.firestore.websocket.WebSocketChannelHandler initChannel\nINFO: Connected to new websocket client\n"}} [2022-10-26T16:08:24.552Z] Oct 26, 2022 10:08:24 AM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead INFO: Detected non-HTTP/2 connection. {"metadata":{"emulator":{"name":"firestore"},"message":"Oct 26, 2022 10:08:24 AM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead\nINFO: Detected non-HTTP/2 connection.\n"}} [2022-10-26T16:08:24.555Z] Oct 26, 2022 10:08:24 AM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead INFO: Detected non-HTTP/2 connection. {"metadata":{"emulator":{"name":"firestore"},"message":"Oct 26, 2022 10:08:24 AM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead\nINFO: Detected non-HTTP/2 connection.\n"}} [2022-10-26T16:08:24.827Z] Oct 26, 2022 10:08:24 AM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead INFO: Detected HTTP/2 connection. {"metadata":{"emulator":{"name":"firestore"},"message":"Oct 26, 2022 10:08:24 AM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead\nINFO: Detected HTTP/2 connection.\n"}} [2022-10-26T16:08:24.857Z] Serving at port 9005
[2022-10-26T16:08:24.877Z] Got response from //functions.yaml Failed to generate manifest from function source: SyntaxError: Cannot use import statement outside a module [2022-10-26T16:08:24.885Z] Failed to parse functions.yamlincomplete explicit mapping pair; a key node is missed; or followed by a non-tabulated empty line at line 1, column 62: ... rom function source: SyntaxError: Cannot use import statement ou ... ^ {"name":"YAMLException","reason":"incomplete explicit mapping pair; a key node is missed; or followed by a non-tabulated empty line","mark":{"name":null,"buffer":"Failed to generate manifest from function source: SyntaxError: Cannot use import statement outside a module\n\u0000","position":61,"line":0,"column":61},"message":"incomplete explicit mapping pair; a key node is missed; or followed by a non-tabulated empty line at line 1, column 62:\n ... rom function source: SyntaxError: Cannot use import statement ou ... \n ^"} [2022-10-26T16:08:24.889Z] shutdown requested via //quitquitquit
⬢ functions: Failed to load function definition from source: FirebaseError: Failed to load function definition from source: Failed to generate manifest from function source: SyntaxError: Cannot use import statement outside a module {"metadata":{"emulator":{"name":"functions"},"message":"Failed to load function definition from source: FirebaseError: Failed to load function definition from source: Failed to generate manifest from function source: SyntaxError: Cannot use import statement outside a module"}}
┌─────────────────────────────────────────────────────────────┐ │ ✔ All emulators ready! It is now safe to connect your app. │ │ i View Emulator UI at http://127.0.0.1:4000/ │ └─────────────────────────────────────────────────────────────┘
┌───────────┬────────────────┬─────────────────────────────────┐ │ Emulator │ Host:Port │ View in Emulator UI │ ├───────────┼────────────────┼─────────────────────────────────┤ │ Functions │ 127.0.0.1:5001 │ http://127.0.0.1:4000/functions │ ├───────────┼────────────────┼─────────────────────────────────┤ │ Firestore │ 127.0.0.1:8080 │ http://127.0.0.1:4000/firestore │ └───────────┴────────────────┴─────────────────────────────────┘ Emulator Hub running at 127.0.0.1:4400 Other reserved ports: 4500, 9150
Issues? Report them at https://github.com/firebase/firebase-tools/issues and attach the *-debug.log files.
[2022-10-26T16:08:25.433Z] Oct 26, 2022 10:08:25 AM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead INFO: Detected non-HTTP/2 connection. {"metadata":{"emulator":{"name":"firestore"},"message":"Oct 26, 2022 10:08:25 AM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead\nINFO: Detected non-HTTP/2 connection.\n"}} [2022-10-26T16:08:25.466Z] Oct 26, 2022 10:08:25 AM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead INFO: Detected non-HTTP/2 connection. {"metadata":{"emulator":{"name":"firestore"},"message":"Oct 26, 2022 10:08:25 AM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead\nINFO: Detected non-HTTP/2 connection.\n"}}