Home > OS >  How to use a private NPM Package in Cloud Firestore?
How to use a private NPM Package in Cloud Firestore?

Time:12-03

I'm using NPM to create a package which typescript classes and methods that are used both in my front-end UI (angular) as well as in my backend mostly google cloud functions.

While I can install the test package in VSCode where I am writing and tedsting my functions, when I try to deploy to Google Firebase Cloud functions (using firebase deploy --only functions, the deploy fails, saying in can't find my NPM package. If I make the package public, it works as expected, but I would really prefer not to make the NPM package public.

Is there some workaround?

CodePudding user response:

If you want to use an NPM package that is not public in your Google Cloud Functions, you will need to host the package yourself. This can be done by publishing the package to a private npm registry such as Verdaccio or Sinopia, or by hosting the package files on a web server and installing them from there.

Here are the steps for publishing your package to a private npm registry:

Install and configure the private npm registry on your server. You can follow the instructions for your chosen registry to set this up. Publish your NPM package to the private registry. You can do this by running the npm publish command and specifying the registry URL as the --registry option. For example:

npm publish --registry=http://my-private-registry.com

Install your package in your Google Cloud Functions project by running the npm install command and specifying the registry URL as the --registry option. For example:

npm install my-private-package --registry=http://my-private-registry.com

Alternatively, you can host the package files on a web server and install them using the npm install command with the --save-exact and --fetch-retries options, as shown below:

npm install http://my-server.com/my-private-package.tgz --save-exact --fetch-retries=5

Using a private npm registry or a web server allows you to keep your NPM package private while still being able to use it in your Google Cloud Functions project. Keep in mind that hosting your own registry or web server will require additional setup and maintenance efforts on your part. It may be easier to simply make your NPM package public, but the decision ultimately depends on your specific requirements and preferences.

  • Related