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

firebase serve: TypeError: Cannot read properties of undefined (reading 'getTime')

Open Deleplace opened this issue 1 year ago • 17 comments

[REQUIRED] Environment info

firebase-tools:

13.8.3

Platform:

Ubuntu 22.04

[REQUIRED] Test case

No project code necessary for repro

[REQUIRED] Steps to reproduce

$ uname -a
Linux my-desktop 6.5.0-18-generic #18~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Wed Feb  7 11:40:03 UTC 2 x86_64 x86_64 x86_64 GNU/Linux
$ brew install firebase-cli
$ firebase init
Select: Hosting
$ firebase serve --only hosting

Then visit http://localhost:5000/ in browser

[REQUIRED] Expected behavior

i  hosting[my-project]: Serving hosting files from: public
✔  hosting[my-project]: Local server: http://localhost:5000

In browser: a proper welcome page

[REQUIRED] Actual behavior

(node:104968) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead.
(Use `node --trace-deprecation ...` to show where the warning was created)
i  hosting[my-project]: Serving hosting files from: public
✔  hosting[my-project]: Local server: http://localhost:5000

TypeError: Cannot read properties of undefined (reading 'getTime')
    at Responder.provider (/home/linuxbrew/.linuxbrew/Cellar/firebase-cli/13.8.3/libexec/lib/node_modules/firebase-tools/node_modules/superstatic/lib/providers/fs.js:89:38)
    at async Promise.all (index 0)
i  hosting: 127.0.0.1 - - [13/May/2024:14:34:57 +0000] "GET / HTTP/1.1" 500 - "-" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:123.0) Gecko/20100101 Firefox/123.0"

In browser: "Unexpected error occurred."

Deleplace avatar May 13 '24 14:05 Deleplace

Hey @Deleplace, thanks for reaching out. I’m trying to reproduce the error you mentioned, but so far, I haven’t encountered any issues with running firebase serve --only hosting. When visiting http://localhost:5000/ , a welcome page is displayed.

image

To help us narrow down what’s causing this, could you let us know which version of Node.js you’re using?

I’m using the ff to try and replicate this:

  • firebase-tools: v13.8.3
  • node: v20.12.2
  • platform: macOS Sonoma 14.4.1

aalej avatar May 13 '24 18:05 aalej

Thank you @aalej for this fast reply.

I edited the issue to mention I had freshly installed the Firebase CLI using brew.

I'm using Node.js v22.1.0

I suspect this specific bug would be easier to reproduce on Linux (filesystem stuff may vary).

It is possible the bug is in superstatic, where this line is dereferencing an fs stats time object which happened to be undefined.

Deleplace avatar May 13 '24 19:05 Deleplace

Thanks for the additional info, @Deleplace. TIL Node.js 22 was released. I tried switching to Node.js v22.1.0, and I was able to reproduce the error. Opening http://localhost:5000/ shows Unexpected error occurred..

Since I’m able to reproduce the issue on a macOS machine, I’m guessing that this could be caused by some changes in Node.js. Could you try temporarily switching to Node.js 20.x.x to see if it would workaround the issue?

Reading through https://nodejs.org/en/about/previous-releases, it looks like Node.js 20 is still the LTS version, while Node.js 22 is scheduled to become LTS around October 2024. I’ll discuss this with our engineering team to see what we could do to address this.

aalej avatar May 14 '24 11:05 aalej

Awesome findings @aalej, this is working for me as well!

(via brew, not via nvm)

$ node -v
v22.1.0
$ brew install node@20
$ brew link --overwrite node@20
$ node -v
v20.13.1
$ firebase serve --only hosting

The Brew formula for firebase-cli currently "depends on node 22.2.0", which is maybe not the best for now.

Deleplace avatar May 16 '24 09:05 Deleplace

I'm not sure of the implications, but it seems superstatic's package.json has

"engines": {
    "node": "18 || 20"
}

while firebase-tools' package.json has

"engines": {
    "node": ">=18.0.0 || >=20.0.0"
}

Deleplace avatar May 16 '24 22:05 Deleplace

Thanks guys, facing the same issue, resolved after reverting node.js back to v20.

sushant3524 avatar May 18 '24 15:05 sushant3524

Also happened to me using node v22 in Debian. Downgraded to v20.

gy-soft avatar Jun 04 '24 03:06 gy-soft

Same on Windows. Fixed by downgrading node from 22 to 20.

ZYinMD avatar Jun 09 '24 19:06 ZYinMD

Same on Mac. As i learned on the comments here, fixed by downgrading from node 22 to 20

snagnever avatar Jun 10 '24 20:06 snagnever

Hey all, I was also facing the same issue. In my case it was a pain to come back of NodeJs version, so literally what I did was to comment line 89 (it's basically a modified timestamp, not a big deal) and started working. Basically commented this line of code: /usr/local/lib/node_modules/firebase-tools/node_modules/superstatic/lib/providers/fs.js:89:38

return {
               //  modified: stat.mtime.getTime(),
                size: stat.size,

juancho088 avatar Jun 24 '24 18:06 juancho088

Sorry for only being able to come around to this now. In the short term, we likely will have to change firebase-tools's engines.node to indicate < 22, since we don't support that officially yet (since it's in active development, not stable). In the long term, we'll have to fix this in superstatic.

bkendall avatar Jun 28 '24 20:06 bkendall

Downgrading node version from 22 to 20 and reinstalling firebase-tools after fixed it for me.

brew install node@20
brew unlink node
brew link node@20

npm install -g firebase-tools

firebase emulators:start

umrashrf avatar Jul 24 '24 13:07 umrashrf

the same is happening to me on node20 (and node18 too)

nvm list
        v9.11.2
       v18.18.2
->     v20.10.0
default -> 20 (-> v20.10.0)
iojs -> N/A (default)
unstable -> N/A (default)
node -> stable (-> v20.10.0) (default)
stable -> 20.10 (-> v20.10.0) (default)
lts/* -> lts/iron (-> v20.10.0)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.17.1 (-> N/A)
lts/carbon -> v8.17.0 (-> N/A)
lts/dubnium -> v10.24.1 (-> N/A)
lts/erbium -> v12.22.12 (-> N/A)
lts/fermium -> v14.21.3 (-> N/A)
lts/gallium -> v16.20.2 (-> N/A)
lts/hydrogen -> v18.18.2
lts/iron -> v20.10.0

% firebase serve --only hosting
(node:23755) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead.
(Use `node --trace-deprecation ...` to show where the warning was created)
i  hosting[sdgsdhsdghsdghsfghs]: Serving hosting files from: public
✔  hosting[sdgsdhsdghsdghsfghs]: Local server: http://localhost:5002
TypeError: Cannot read properties of undefined (reading 'getTime')
    at Responder.provider (/opt/homebrew/Cellar/firebase-cli/13.15.4/libexec/lib/node_modules/firebase-tools/node_modules/superstatic/lib/providers/fs.js:89:38)
    at async Promise.all (index 0)
i  hosting: 127.0.0.1 - - [26/Aug/2024:14:28:11 +0000] "GET / HTTP/1.1" 500 - "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36"
TypeError: Cannot read properties of undefined (reading 'getTime')
    at Responder.provider (/opt/homebrew/Cellar/firebase-cli/13.15.4/libexec/lib/node_modules/firebase-tools/node_modules/superstatic/lib/providers/fs.js:89:38)
i  hosting: 127.0.0.1 - - [26/Aug/2024:14:28:11 +0000] "GET /favicon.ico HTTP/1.1" 500 - "http://localhost:5002/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36"

barii avatar Aug 26 '24 14:08 barii

@barii

% firebase serve --only hosting
(node:23755) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead.

Looks like you're still running firebase with Node 22.

For me, I had to install firebase a second time in the proper virtual environment.

eddieh avatar Sep 20 '24 01:09 eddieh

For me, "brew install firebase-cli" automatically installs node 22.9 with it, how to get around that in venv? @eddieh

hanspeteroliver avatar Sep 21 '24 20:09 hanspeteroliver

Personally I hate brew and nvm and both should be abandoned with extreme prejudice. I used the python virtual environment with nodeenv. It may be a bit more complex than other solutions, but it nicely contains everything in the project directory.

% python3 -m venv .env
% source .env/bin/activate
(.env) % pip install nodeenv

Then (not sure I picked the best node version, but it was a recent one with a point release):

(.env) % nodeenv --node=20.15.1 .nv20
(.env) % source .nv20/bin/activate
(.nv20) (.env) % npm install -g firebase-tools

Then

(.nv20) (.env) % firebase login
(.nv20) (.env) % firebase init

etc.

Maybe not the most elegant solution, but it is all in the project directory and the rest of my system is unchanged. No new version of node, firebase, or stupid shell functions (nvm, I'm looking at you).

eddieh avatar Sep 22 '24 00:09 eddieh

I was facing the same issue on Windows, solved by downgrading to nodejs 20 using nvm. I used Chocolatey to install nvm, here are my steps:

Install nvm using choco: choco install nvm

Install version 20: nvm install 20

Change to new installed version: nvm use 20

Verify current node version being used: node -v

Restart the CMD and rerun firebase.

andersonbelli avatar Oct 20 '24 17:10 andersonbelli

FYI - People using the latest Node LTS may start experiencing this issue as Node LTS just switched from v20 to v22 (https://github.com/nodejs/Release?tab=readme-ov-file#release-schedule).

(My tests just started to fail in Github actions as actions/setup-node@v4 was configured with node-version: lts/*; forcing Node v20 by node-version: 20 resolved the issue.)

petrkotek avatar Nov 04 '24 04:11 petrkotek

I have a manual solve that keeps the modified time.

You have to edit the file in question, and then on line 89 change from:

modified: stat.mtime.getTime(),

to

modified: stat.mtimeMs,

and thus the return statement will look like:

return {
                modified: stat.mtimeMs,
                size: stat.size,
                etag: await fetchEtag(stat.pathname, stat),
                stream: fs.createReadStream(stat.pathname),
            };

And then recompile/redeploy.

PiotrKrzyzek avatar Nov 06 '24 16:11 PiotrKrzyzek

This was just fixed in superstatic https://github.com/firebase/superstatic/releases/tag/v9.1.0

f0urfingeredfish avatar Nov 13 '24 19:11 f0urfingeredfish