How to use ES6(esm) imports/exports in cloud functions

做~自己de王妃 提交于 2020-12-05 11:56:51

问题


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:

  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


回答1:


"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')


来源:https://stackoverflow.com/questions/59904599/how-to-use-es6esm-imports-exports-in-cloud-functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!