2

In my firebase functions, I created a local package to share code in my angular application and my firebase functions. This seems to break the deploying of my code, as you will see in the log on the bottom.

The folder structure is like this:

/frontend/..
/backend/functions
/shared/..

The package.json of my firebase functions has this entry:

"my-shared": "file:../../shared",

When I import the code from my-shared, it looks like this:

import { User } from "my-shared/lib/models/user";

The package.json from the shared folder looks like this:

{
  "name": "my-shared",
  "version": "1.0.0",
  "main": "lib/index.js",
  "scripts": {
    "build": "tsc",
    "prepare": "npm run build",
    "watch": "npx tsc --watch"
  },
  "devDependencies": {
    "typescript": "^2.8.3"
  },
  "files": [
    "lib/**/*"
  ],
  "dependencies": {
    "json2typescript": "^1.5.0-rc.0"
  },
  "private": true
}

When I run firebase deploy, I got this error:

Error: Functions did not deploy properly.

Here a snippet of the log when running this:

firebase functions:log --only myFunction

Build failed: npm ERR! code ENOENT\nnpm ERR! syscall open\nnpm ERR! path /workspace/node_modules/shared/package.json\nnpm ERR! errno -2\nnpm ERR! enoent ENOENT: no such file or directory, open '/workspace/node_modules/shared/package.json'\nnpm ERR! enoent This is related to npm not being able to find a file.\nnpm ERR!

What I did so far:

  • npm install in shared
  • rm -rf node_modules && npm rm package-lock.json && npm install in backend/functions
  • for testing I replaced the absolute path of "my-shared": "file:../../shared"
  • tried npm link, but as I understand it correctly the line above should also work
  • update node globally
  • spend much time to solve this problem

Is something wrong with my local package or is there a configuration missing? Would be great, if someone could help me here.

By the way, the frontend has the same package.json entry and is compiling successfully.

Yetispapa
  • 2,174
  • 2
  • 30
  • 52
  • 1
    Does this answer your question? [Using a shared node module for common classes](https://stackoverflow.com/a/58877253/3068190). While the linked question targets TypeScript, for future readers the steps taken should work fine even for JavaScript. The key point to consider is that symbolic links and code outside of the `/backend/functions` directory will not be copied to the server which means it will be unavailable. – samthecodingman Sep 17 '21 at 13:30
  • 1
    Hi @samthecodingman, based on your suggestion, I tried the solution which they mentioned with the copying of code in the predeploy script. The solution is promising, but I have the problem, when I run my emulator it doesn't work, cause the code for compiling is only copied when the predeploy script is executed. Normally I have npx tsc --watch running and in parallel the emulator. So it seems, my emulator is not working. Or do I miss something? You have experience with Typescript? – Yetispapa Sep 19 '21 at 11:28
  • 1
    I have the issue: Did you find a solution? – Ero Stefano Dec 20 '21 at 00:02

0 Answers0