Firebase-tools: Can't run firebase serve --only functions any more.

Created on 10 Apr 2018  路  8Comments  路  Source: firebase/firebase-tools

Hello there,
Unfortunately I have to reopen this issues... it seems very similar to previous ones, but the versions are different with the latest updates and the solutions simply don't fix the problem.

Issue:
When I try to use firebase serve --only functions it always displays the error:
Failed to load functions source code. Ensure that you have the latest SDK by running npm...~
and
Error from emulator. Error parsing triggers: cannot find module 'PATH_TO_FUNCTIONS'

I tried running npm install as it suggests, removing node_modules, doing it again, as administrator, clearing the cache also, refreshing the global installation. Changing the node version, swapping to another version of firebase-functions, admin and tools...

I simply don't know what I'm doing wrong. I've been three days messing around with this and I even tried 3 different machines already: two Windows 10 (one as elevated administrator) and an the latest Ubuntu.

Please, could somebody help me?

Thanks!

Version info

Node: 6.14.1
npm: 3.10.10
firebase-tools: 3.18.2
firebase-admin: ~5.12.0
firebase-functions: ^1.0.1

Platform Information

Latest Ubuntu, but I also tested with latest built on Windows 10

Steps to reproduce

Very simple:
Create a new project on firebase console.
Install your environment (firebase-tools...)
Log into your account with firebase login
Initialize your project in your directory with firebase init
Use your existing new project as only functions kind of project
Keep saying yes pretty much to everything the assistant asks
uncomment in index.ts inside functions the three lines "export const helloWorld..." and save it
go into functions dir and run npm install
when finished simply run firebase serve --only functions
you get the error message...

Expected behavior

I would say that, since I'm strictly following the step-by-step guide, it simply should work as it used to.

Actual behavior

Error message that actually doesn't tell much. So I don't know what I'm missing...

Thank you very much!
Any help it is very appreciated!

Most helpful comment

It seems like you are using TypeScript, which does require a compile step prior to emulation. You can experiment with using watch mode to build your files by running tsc --watch, this will recompile your files every time you change something. And then in a separate tab of the terminal, run firebase serve.

If you find all of this to be too much work and don't have a particular need for tyescript, you can switch to using JavaScript instead since JavaScript does not require compiling. (Make sure you select "JavaScript" when running "firebase init")

All 8 comments

In case it helps: I've been able to find something else, here is it:

If I run npm run build it builds the application, without errors. It compiles it and places it where it was supposed to be placed by firebase serve --only functions command. However, that npm command by itself won't spawn the server. If after having it compiled I run firebase serve --only functions it goes ahead without errors... maybe that can give somebody a hint of what is going on...
This workaround doesn't make me happy. Every single little change in my triggers forces me to stop, go to the terminal, run npm run build wait until it finishes and then run firebase serve --only functions again since it won't detect changes automatically and recompile as it used to do not long ago...

I don't know if I'm missing something... it must be so obvious I'm skipping it all the time.

Please, help...
:-(

Same issue here on OSX 10.13.1, also on a fresh project, also using the same firebase package versions as @supermarcos.

I tried removing and re-adding google-cloud/functions-emulator, which was on 1.0.0-alpha.22 previously and got bumped to 1.0.0-beta.4. No change in error behaviour.

node v9.11.1
yarn 1.5.1

It seems like you are using TypeScript, which does require a compile step prior to emulation. You can experiment with using watch mode to build your files by running tsc --watch, this will recompile your files every time you change something. And then in a separate tab of the terminal, run firebase serve.

If you find all of this to be too much work and don't have a particular need for tyescript, you can switch to using JavaScript instead since JavaScript does not require compiling. (Make sure you select "JavaScript" when running "firebase init")

You can learn more about --watch and other typescript compiler options here: https://www.typescriptlang.org/docs/handbook/compiler-options.html

Thanks @laurenzlong !
That's a nice explanation... I was suspecting there must be something related with the build process not being triggered properly from the firebase serve --only functions command, but I would swear that was working before without need of having one terminal to compile and another to serve...
Has been there a change somewhere I didn't notice on this?

In any case, your workaround works. I actually changed my package.json to have it easier so I can run npm build and it will run tsc with --watch.

I think also it would be great to find out if there is a way to run tsc --watch and concatenate with firebase serve --only functions... I guess it is not possible right now becasuse --watch won't finish the program so the second one can't run until it gets an exit from the first. Am I wrong?

Thank you again!
Very helpful!

P.S: did it work for you @papertokyo ?

Glad it's working for you! We haven't changed the behavior of firebase serve recently, perhaps you were using JavaScript before? I'm also not sure if there's away to run both in one command and have them work properly, haven't done enough experimentations with it myself. Let me know if you figure something out! Since in that case, we can populate the default package.json with it as the "serve" script.

I guess the change maybe was in the typescript compiler, because I can definitely confirm that I've been using typescript in the project from the beginning and running just firebase serve --only functions was working around a week ago.

I've been reading about how to run npm commands in parallel here:
https://stackoverflow.com/questions/30950032/how-can-i-run-multiple-npm-scripts-in-parallel
It seems there are different approaches but they have their pros and cons such not all of them work in all the platforms, etc... I can confirm that the npm run start... trick works for windows, but not for linux whereas using a single ampersand to concatenate commands will work for linux, but not for windows and, also, you have to be careful because the command that gets pushed into background (the first one) will become a zombie so you need to kill its processes every time you want to re-run the command.

I hope this helps somebody.

Interesting, thanks for sharing!

Was this page helpful?
0 / 5 - 0 ratings