Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import default issue with babel@6 #33

Closed
stevenyap opened this issue Apr 12, 2017 · 5 comments
Closed

Import default issue with babel@6 #33

stevenyap opened this issue Apr 12, 2017 · 5 comments

Comments

@stevenyap
Copy link

Firebase-functions version is 0.5.4

I am using babel@6 to transpile my es6 code and found an issue that the transpiled code is not working. Here's how to reproduce:

import functions from 'firebase-functions'

export const helloWorld = functions.https.onRequest((request, response) => {
  response.send('Hello World!')
})

which is transpiled into (using babel@6 es2015 preset):

'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.helloWorld = undefined;

var _firebaseFunctions = require('firebase-functions');

var _firebaseFunctions2 = _interopRequireDefault(_firebaseFunctions);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var helloWorld = exports.helloWorld = _firebaseFunctions2.default.https.onRequest(function (request, response) {
  response.send('Hello World!');
});

which produces the following error when deploying:

i  deploying functions
i  functions: ensuring necessary APIs are enabled...
i  runtimeconfig: ensuring necessary APIs are enabled...
  runtimeconfig: all necessary APIs are enabledfunctions: all necessary APIs are enabled
i  functions: preparing functions directory for uploading...

Error: Error occurred while parsing your function triggers.

TypeError: Cannot read property 'https' of undefined
    at Object.<anonymous> (/private/var/folders/6q/__dzccnj1p7_k7vz2qp2ntdr0000gn/T/fbfn_21971bAqbpyOqCko0/index.js:14:66)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.require (module.js:498:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/usr/local/lib/node_modules/firebase-tools/lib/triggerParser.js:16:9)
    at Module._compile (module.js:571:32)

I think the issue is that firebase-functions did not export a default and hence, when importing a default (import functions from 'firebase-functions'), the transpiled code does not work. This could be related to babel's T2212 Kill CommonJS default export behaviour.

A quick workaround is to import the individual package instead of importing the default:

import { https } from 'firebase-functions'

export const helloWorld = https.onRequest((request, response) => {
  response.send('Hello World!')
})

which transpiles into:

'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.helloWorld = undefined;

var _firebaseFunctions = require('firebase-functions');

var helloWorld = exports.helloWorld = _firebaseFunctions.https.onRequest(function (request, response) {
  response.send('Hello World!');
});

which has no issue to run and to deploy.

The fix should be a relatively easy one which is to explicitly export a default in firebase-functions.

@mbleigh
Copy link
Contributor

mbleigh commented Apr 12, 2017 via email

@achuthhadnoor
Copy link

i deploying functions
i functions: ensuring necessary APIs are enabled...
i runtimeconfig: ensuring necessary APIs are enabled...
✔ runtimeconfig: all necessary APIs are enabled
✔ functions: all necessary APIs are enabled
i functions: preparing functions directory for uploading...

Error: Error occurred while parsing your function triggers.

SyntaxError: Unexpected token =
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:373:25)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Object. (/tmp/fbfn_36143HpdzU1HAk5MB/node_modules/firebase-functions/lib/index.js:28:19)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)

may i know why am i getting this

@justinrosenthal
Copy link
Contributor

Hi @achuthhadnoor, which version of node are you using? Can you paste here the result of running node --version?

@achuthhadnoor
Copy link

Hi @justinrosenthal the node version I use is v4.4.5

@justinrosenthal
Copy link
Contributor

@achuthhadnoor That's likely why you're having problems. You should upgrade (or install via nvm) to Node v6.11.1 as mentioned in the Getting Started guide.

v6 supports many more language features than v4, you can find more information here: http://node.green/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants