GoogleCloudPlatform / functions-framework-nodejs

FaaS (Function as a service) framework for writing portable Node.js functions
Apache License 2.0
1.29k stars 158 forks source link

ESM packages not getting deployed on cloud functions even though package.json having type=module #454

Closed ssanyal closed 2 years ago

ssanyal commented 2 years ago

package.json { "name": "api", "version": "1.0.0", "exports": "./dist/index.js", "type": "module",

"scripts": { "start": "ts-node ./src/index.ts", "lint": "eslint --ext .js,.ts .", "build": "tsc", "dev": "yarn build && functions-framework --target=api --runtime nodejs14 --source=./dist/", "deploy": "yarn build && gcloud functions deploy api --runtime nodejs14 --trigger-http --source=./dist/ --project $GCP_PROJECTID", } }

this does not work with node 16 as well , locally the function runs fine with functions-framework , but cloud functions deploy fails with ERROR: (gcloud.functions.deploy) OperationError: code=3, message=Build failed: (node:55) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.

grant commented 2 years ago

Thanks for the issue Subhajyoti.

Can you provide the full repro steps? What is your index file?

It looks like you have a build error.


Does this tutorial work for you?

https://github.com/GoogleCloudPlatform/functions-framework-nodejs/blob/master/docs/esm.md

ssanyal commented 2 years ago

{ "name": "api", "version": "1.0.0", "description": "Backend App", "exports": "./dist/index.js", "license": "MIT", "type": "module", "main": "dist/index.js", "types": "dist/index.d.ts", "dependencies": { @./functions-framework": "^3.1.0", @./logging-bunyan": "^3.3.0", @./opentelemetry-cloud-trace-exporter": "^1.1.0", @./api": "^1.0.4", @./sdk-trace-base": "^1.0.1", @./sdk-trace-node": "^1.0.1", @./morgan": "^1.9.3", @./node": "^17.0.23", "axios": "^0.26.1", "body-parser": "^1.20.0", "bunyan": "^1.8.15", "cors": "^2.8.5", "express": "^4.17.3", "firebase": "^9.6.10", "firebase-admin": "^10.0.2", "firebase-functions": "^3.20.0", "from": "^0.1.7", "import": "^0.0.6", "joi": "^17.6.0", "lb": "^0.0.1", "morgan": "^1.10.0", "opentelemetry": "^0.1.0", "typescript": "^4.7.0-dev.20220405" }, "devDependencies": { "eslint": "^8.12.0", "firebase-functions-test": "^0.3.3", "ts-node": "^10.7.0", "yarn-audit-fix": "^9.2.2" }, "scripts": { "start": "ts-node ./src/index.ts", "lint": "eslint --ext .js,.ts .", "build": "tsc", "dev": "yarn build && functions-framework --target=api --runtime nodejs14 --source=./dist/", "shell": "yarn build && firebase functions:shell", "serve": "yarn build && firebase emulators:start --only functions", "deploy": "yarn build && gcloud functions deploy api --runtime nodejs14 --trigger-http --source=./dist/ --project $GCP_PROJECTID", "logs": "firebase functions:log", "firedeploy": "firebase deploy --only functions" } } this is the package.json and below is the index.ts

import { config, https } from "firebase-functions"; import { cors } from "./middlewares/cors.js"; import morgan from "morgan"; import admin from "firebase-admin"; import express from "express"; import bodyParser from "body-parser"; import firebase from "firebase/compat/app"; import { morganlog } from "./middlewares/morganlog.js"; import lb from @./logging-bunyan"; import opentelemetry from @./api"; import { NodeTracerProvider } from @./sdk-trace-node"; import { SimpleSpanProcessor } from @./sdk-trace-base"; import { TraceExporter } from @.**/opentelemetry-cloud-trace-exporter"; const provider = new NodeTracerProvider(); const exporter = new TraceExporter(); provider.addSpanProcessor(new SimpleSpanProcessor(exporter)); const app = express(); const { logger, mw } = await lb.express.middleware(); app.use(mw); app.use(morgan(morganlog)); try { admin.initializeApp(); firebase.initializeApp(config().firebase); console.log(config is ${config().firebase}); app.use(bodyParser.json()); app.all("/", cors); } catch (err: any) { console.log(err.message); throw err; } app.get("/", (req: any, res: any) => { // req.log can be used as a bunyan style log method. All logs generated // using req.log use the current request context. That is, all logs // corresponding to a specific request will be bundled in the Cloud UI. req.log.info("this is an info log message"); res.send("hello world"); }); / userRoutes(app); productRoutes(app); attributeRoutes(app); addonGroupRoutes(app); categoryRoutes(app); imageRoutes(app); orderRoutes(app); paymentRoutes(app); displaycardRoutes(app); siteRoutes(app); storeRoutes(app); / const api = https.onRequest(app); export { api }; // exports.createOrder = createOrder; // exports.updateOrder = updateOrder;

grant commented 2 years ago

Thanks for the prompt response.

However, can you please provide a minimal setup for the issue? Did the above links work for you?

I'm going to close this issue since it is not reproducible, but feel free to comment and we can re-open if requested.