firebase-functions icon indicating copy to clipboard operation
firebase-functions copied to clipboard

Error: Unexpected token import when running firebase deploy

Open lcoppinger opened this issue 8 years ago • 3 comments

Version info

firebase-functions: 1.0.2

firebase-tools: 3.18.4

firebase-admin: 5.12.0

Test case

Run firebase deploy in console.

My functions/package.json { "name": "functions", "scripts": { "compile_@amcharts_amcharts3-angular": "babel node_modules/@amcharts/amcharts3-angular -d node_modules/@amcharts/amcharts3-angular --presets es2015", "compile_ngx-google-places-autocomplete": "babel node_modules/ngx-google-places-autocomplete -d node_modules/ngx-google-places-autocomplete --presets es2015", "postinstall": "npm run compile_@amcharts_amcharts3-angular && npm run compile_ngx-google-places-autocomplete", "build": "tsc", "serve": "npm run build && firebase serve --only functions", "shell": "npm run build && firebase functions:shell", "start": "npm run shell", "deploy": "firebase deploy --only functions", "logs": "firebase functions:log" }, "main": "lib/index.js", "dependencies": { "@amcharts/amcharts3-angular": "^1.2.1", "@angular/animations": "5.0.0-rc.5", "@angular/cdk": "^5.2.5", "@angular/cli": "^1.6.8", "@angular/common": "5.0.0-rc.5", "@angular/compiler": "5.0.0", "@angular/core": "5.0.0-rc.5", "@angular/flex-layout": "^5.0.0-beta.14", "@angular/forms": "5.0.0-rc.5", "@angular/http": "5.0.0-rc.5", "@angular/platform-browser": "^5.0.0-rc.0", "@angular/platform-browser-dynamic": "5.0.0-rc.5", "@angular/platform-server": "^5.2.10", "@angular/router": "5.0.0-rc.5", "@firebase/app": "^0.3.1", "@firebase/database": "^0.3.1", "@fortawesome/fontawesome-svg-core": "^1.2.0-11", "@fortawesome/free-brands-svg-icons": "^5.1.0-8", "@fortawesome/free-solid-svg-icons": "^5.1.0-8", "@nguniversal/module-map-ngfactory-loader": "^5.0.0", "@ngx-share/core": "^5.0.1", "@types/angular-google-analytics": "^1.1.33", "@types/d3": "^5.0.0", "@types/express": "^4.11.1", "@types/google.analytics": "0.0.36", "angularfire2": "^5.0.0-rc.7.2-next", "babel-cli": "^6.26.0", "babel-preset-es2015": "^6.24.1", "bufferutil": "^3.0.4", "core-js": "^2.4.1", "d3": "^4.9.1", "d3-geo": "^1.10.0", "d3-queue": "^3.0.7", "express": "^4.16.3", "firebase": "^4.13.1", "firebase-admin": "~5.12.0", "firebase-functions": "^1.0.2", "font-awesome": "^4.7.0", "jquery": "^3.2.1", "ngx-google-places-autocomplete": "^2.0.1", "ngx-slick": "^0.1.3", "rxjs": "^5.5.10", "topojson": "^3.0.0", "typescript": "^2.4.2", "utf-8-validate": "^4.0.1", "web-animations-js": "^2.3.1", "ws": "^5.1.1", "xmlhttprequest": "^1.8.0", "zone.js": "^0.8.14" }, "devDependencies": { "typescript": "^2.5.3" }, "private": true } My firebase.json { "hosting": { "public": "dist", "ignore": [ "firebase.json", "**/.*", "**/node_modules/**" ], "rewrites": [ { "source": "**", "function": "ssrapp" } ] }, "functions": { "predeploy": "npm --prefix \"$RESOURCE_DIR\" run build" } }

functions/src/index.ts `import 'zone.js/dist/zone-node'; import * as functions from 'firebase-functions'; import * as express from 'express'; import { renderModuleFactory } from '@angular/platform-server'; import * as fs from 'fs';

// // Start writing Firebase Functions // // https://firebase.google.com/docs/functions/typescript // // export const helloWorld = functions.https.onRequest((request, response) => { // response.send("Hello from Firebase!"); // }); const document = fs.readFileSync(__dirname + '/index.html', 'utf8'); const AppServerModuleNgFactory = require(__dirname + '/dist-server/main.bundle').AppServerModuleNgFactory;

const app = express();

app.get('**', (req, res) => { const url = req.path; renderModuleFactory(AppServerModuleNgFactory, { document, url }) .then(html => { res.set('Cache-Control', 'public, max-age=600, s-maxage=1200') res.send(html); }); });

export let ssrapp = functions.https.onRequest(app);`

Steps to reproduce

npm i to install dependencies in functions dir run firebase deploy in console.

Were you able to successfully deploy your functions?

No

Console error ` ✔ functions: Finished running predeploy script. i functions: ensuring necessary APIs are enabled... ✔ functions: all necessary APIs are enabled i functions: preparing functions directory for uploading... i functions: packaged functions (56.28 MB) for uploading ✔ functions: functions folder uploaded successfully i hosting: preparing dist directory for upload... ✔ hosting: 151 files uploaded successfully i functions: updating function ssrapp... ⚠ functions[ssrapp]: Deployment error. Function load error: Code in file lib/index.js can't be loaded. Is there a syntax error in your code? Detailed stack trace: /user_code/node_modules/@amcharts/amcharts3-angular/es2015/index.js:1 (function (exports, require, module, __filename, __dirname) { import { Directive, ElementRef, Input, NgZone, NgModule, Injectable } from "@angular/core"; ^^^^^^

SyntaxError: Unexpected token import at createScript (vm.js:56:10) at Object.runInThisContext (vm.js:97:10) at Module._compile (module.js:549:28) at Object.Module._extensions..js (module.js:586:10) at Module.load (module.js:494:32) at tryModuleLoad (module.js:453:12) at Function.Module._load (module.js:445:3) at Module.require (module.js:504:17) at require (internal/module.js:20:19) at Object.Utb9 (/user_code/lib/dist-server/main.bundle.js:1:196761)

Functions deploy had errors. To continue deploying other features (such as database), run: firebase deploy --except functions

Error: Functions did not deploy properly.`

Expected behavior

I think the problem here is that Cloud Functions is downloading dependencies from the package.json file as stated here: https://firebase.google.com/docs/functions/handle-dependencies

The problem is two of my node modules are in ES6 which isn't understood by Node.js. On my local I use the babel compiler at the top of my package.json and everything works fine.

I would either like to be able to do this remotely during the deploy or force the deploy to bundle and use my local node modules.

Actual behavior

` ✔ functions: Finished running predeploy script. i functions: ensuring necessary APIs are enabled... ✔ functions: all necessary APIs are enabled i functions: preparing functions directory for uploading... i functions: packaged functions (56.28 MB) for uploading ✔ functions: functions folder uploaded successfully i hosting: preparing dist directory for upload... ✔ hosting: 151 files uploaded successfully i functions: updating function ssrapp... ⚠ functions[ssrapp]: Deployment error. Function load error: Code in file lib/index.js can't be loaded. Is there a syntax error in your code? Detailed stack trace: /user_code/node_modules/@amcharts/amcharts3-angular/es2015/index.js:1 (function (exports, require, module, __filename, __dirname) { import { Directive, ElementRef, Input, NgZone, NgModule, Injectable } from "@angular/core"; ^^^^^^

SyntaxError: Unexpected token import at createScript (vm.js:56:10) at Object.runInThisContext (vm.js:97:10) at Module._compile (module.js:549:28) at Object.Module._extensions..js (module.js:586:10) at Module.load (module.js:494:32) at tryModuleLoad (module.js:453:12) at Function.Module._load (module.js:445:3) at Module.require (module.js:504:17) at require (internal/module.js:20:19) at Object.Utb9 (/user_code/lib/dist-server/main.bundle.js:1:196761)

Functions deploy had errors. To continue deploying other features (such as database), run: firebase deploy --except functions

Error: Functions did not deploy properly.`

lcoppinger avatar May 14 '18 20:05 lcoppinger

We currently do not support non-Node modules in the regular workflow. As a workaround, you can build the dependency locally and put it inside a folder (and name it something other than node_modules). Then in package.json, refer to it:

"dependencies": {
 "desiredModule": "./myModulesFolder/desiredModule"
} 

This is a bit hacky, so I'm not sure if it'll work well, but it's worth a try.

laurenzlong avatar May 14 '18 20:05 laurenzlong

@laurenzlong The workaround you have suggested doesnt seem to be working. Is there any other alternatives?

ananthu-confianz avatar Jun 17 '19 12:06 ananthu-confianz

@ananthu-confianz this workaround does work for me - please open another issue with the required details if you are still having trouble and require further help.

thechenky avatar Jun 17 '19 16:06 thechenky