0

I've got a Firebase project with file structure like so:

node_modules/
package.json
functions/
  node_modules/
  package.json

If I npm install firebase in the parent project, everything works fine locally but the code fails on deployment because the child project is deployed without the parent project yet depends on Firebase.

If I install Firebase in the child project, the parent app complains "Cannot find module 'firebase/storage'"

If I install Firebase in both projects, the parent app complains "Cannot read 'path' of undefined" - debugging shows this is a Firebase internal error.

My ideal solution is to npm install firebase in the child project only, but somehow allow the parent project to access it? Is this possible?

I've read about npm-link but not sure if this can help me or whether there is some other more suitable solution.

Optional reading: Why do I have a structure like this?

I ended up with this structure because the child project is a Firebase project and firebase init created the functions folder and its contents. The child project is deployed to Google Cloud.

The parent project is a simple test bed app that I created and uses much of the code in the child project for testing purposes only, this code is not deployed.

halfer
  • 19,824
  • 17
  • 99
  • 186
danday74
  • 52,471
  • 49
  • 232
  • 283

1 Answers1

1

Got the answer from here:

how to specify local modules as npm package dependencies

I ran this command in the parent project:

npm i --save ./functions/node_modules/firebase

Which adds an entry to the parent package.json as follows:

"firebase": "file:functions/node_modules/firebase"

Now everything works!

Obviously, this assumes that the child package is actually installed.

Using a preinstall script in the parent package.json to install the child packages first ensures this works smoothly.

My preinstall script entry looks like this:

"preinstall": "sh preinstall.sh"

And the script itself has the following contents:

#!/usr/bin/env bash

rm -rf node_modules

cd functions || exit

rm -rf node_modules
npm install --also=dev

When you run npm install the preinstall script is automatically executed before the actual install takes place.

danday74
  • 52,471
  • 49
  • 232
  • 283