最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to use ES6(esm) importsexports in cloud functions - Stack Overflow

programmeradmin0浏览0评论
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._pile (internal/modules/cjs/loader.js:721:23)

Caveats

I'm using third party libraries(@utilModuler) which were written via import/exports. Possible workarounds:

  1. Fork library and generate cjs file with rollup.
  2. esm works like a charm but it cause unnesecary memory consumptions

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

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._pile (internal/modules/cjs/loader.js:721:23)

Caveats

I'm using third party libraries(@utilModuler) which were written via import/exports. Possible workarounds:

  1. Fork library and generate cjs file with rollup.
  2. esm works like a charm but it cause unnesecary memory consumptions

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

Share Improve this question edited Jan 25, 2020 at 15:18 Bergi 665k161 gold badges1k silver badges1.5k bronze badges asked Jan 24, 2020 at 22:41 Igor KokotkoIgor Kokotko 5091 gold badge5 silver badges14 bronze badges 5
  • No need to fork the library to transpile it? – Bergi Commented Jan 25, 2020 at 15:17
  • You may want to use webpack or Babel. How is your project structured? – Andre M Commented Jan 25, 2020 at 15:35
  • Yeah actually rollup, webpack and Bable could handle this. Structure: --package -- UtilModuler --package -- ... -- src(vue.js) -- functions(google cloud functions) Packages could use for frontend and backend. – Igor Kokotko Commented Jan 26, 2020 at 16:02
  • what was your solution at the end of the day? Facing the same thing, – vir us Commented Oct 7, 2020 at 11:43
  • I used rollup to transpile them into cjs modules. – Igor Kokotko Commented Oct 7, 2020 at 14:16
Add a ment  | 

4 Answers 4

Reset to default 7

It looks like, ESM support has been added by the latest version of the firebase CLI (https://github./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

  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",
    ...
  },

and an example function:

// index.js
import functions from "firebase-functions";

export const helloWorld = functions.https.onRequest((request, response) => {
  response.send("Hello from Firebase!");
});
"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')

/index.js

// This is so cloud functions work
// @ts-ignore
export * from './build/index.js';

My code is in typescript in ./src dir, piles out to ./build . So I just added that index.js file to my root dir and that worked for me.

in my case export the function from this

module.exports = { helloFunction };

to I changed to below worked for me helloFunction.ts

import { onRequest } from "firebase-functions/v2/https";

export const helloFunction = onRequest((req, res) => {
  res.send("Hello, Firebase Functions!");
});

and imported in index.ts as

export { helloFunction } from "./helloFunction";
发布评论

评论列表(0)

  1. 暂无评论