import functions from 'firebase-functions';
import UtilModuler from '@utilModuler'
exports.helloWorld = functions.https.onRequest((request, response) => {
response.send("Hello from Firebase!");
});
import UtilModuler from '@utilModuler'; ^^^^^^^^^
SyntaxError: Unexpected identifier at Module._compile (internal/modules/cjs/loader.js:721:23)
Caveats
I'm using third party libraries(@utilModuler) which were written via import/exports. Possible workarounds:
Question : is there are a way how to use hybrid import cjs and esm in google cloud function?(except options which I described above)
Would be nice to use in deploy function something like --experimental-modules
"devDependencies": {
"@babel/core": "^7.2.0",
"@babel/preset-env": "^7.2.0",
"@babel/register": "^7.0.0"
}
.babelrc
{
"presets": ["@babel/preset-env"]
}
entry point node.js app
require("@babel/register")({})
// Import the rest of our application.
module.exports = require('./index.js')
It looks like, ESM support has been added by the latest version of the firebase CLI (https://github.com/firebase/firebase-tools/releases/tag/v9.15.0 ):
$ npm install -g firebase-tools # Get the latest firebase-cli
$ firebase deploy --only functions # Deploy as usual
and
// package.json
...
"engines": {
"node": "14"
},
"type": "module",
"dependencies": {
"@google-cloud/functions-framework": "^1.9.0",
...
},
and an example function:
// index.js
import functions from "firebase-functions";
export const helloWorld = functions.https.onRequest((request, response) => {
response.send("Hello from Firebase!");
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.