Home > Software engineering >  Installing private GitHub npm package in Firebase Cloud Functions
Installing private GitHub npm package in Firebase Cloud Functions

Time:11-05

I am new to Firebase Cloud Functions and have been struggling with adding private npm packages to make my functions work. I understand Firebase will treat them all as public unless specified and will install with npm / yarn what I have in package.json.

The only way for me to tell Firebase that it's a private repository on Github is to add a

.npmrc (containing) - The key I am using is a Personal Access Token from Github-Developers that has all the need it permissions

//npm.pkg.github.com/:_authToken=<access token>
# @calugarul:registry=git https://npm.pkg.github.com/calugarul
@calugarul:registry=git https://<access token>@github.com/kroitor/ccxt.pro.git

In my index.js file I am calling the private repo (ccxt.pro) but it will not install on the Firebase Cloud Functions server.

index.js

const functions = require("firebase-functions");

var num = 1;

const admin = require('firebase-admin');
admin.initializeApp();

const db = admin.firestore();

var moment = require('moment'); 
const ccxtpro = require ('ccxt.pro');

const coinbaseEx = new ccxtpro.coinbasepro({'enableRateLimit': true});

package.json (contains)

{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "scripts": {
    "lint": "eslint",
    "serve": "firebase emulators:start --only functions",
    "shell": "firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "engines": {
    "node": "14"
  },
  "main": "index.js",
  "dependencies": {
    "@calugarul/ccxt.pro": "git https://github.com/kroitor/ccxt.pro.git",
    "firebase-admin": "^9.8.0",
    "firebase-functions": "^3.16.0",
    "moment": "^2.29.1"
  },
  "devDependencies": {
    "eslint": "^7.6.0",
    "eslint-config-google": "^0.14.0",
    "firebase-functions-test": "^0.2.0"
  },
  "private": true
}

Usual Error:

 E getTickersAndSendRequestToStart: {"@type":"type.googleapis.com/google.cloud.audit.AuditLog","status":{"code":3,"message":"Build failed: npm ERR! Error while executing:\nnpm ERR! /usr/bin/git ls-remote -h -t https://github.com/kroitor/ccxt.pro.git\nnpm ERR! \nnpm ERR! remote: Support for password authentication was removed on August 13, 2021. Please use a personal access token instead.\nnpm ERR! remote: Please see https://github.blog/2020-12-15-token-authentication-requirements-for-git-operations/ for more information.\nnpm ERR! fatal: Authentication failed for 'https://github.com/kroitor/ccxt.pro.git/'\nnpm ERR! \nnpm ERR! exited with error code: 128\n\nnpm ERR! A complete log of this run can be found in:\nnpm ERR!     /www-data-home/.npm/_logs/2021-10-31T02_03_39_782Z-debug.log; Error ID: beaf8772"},"authenticationInfo":{"principalEmail":"my email"},"serviceName":"cloudfunctions.googleapis.com","methodName":"google.cloud.functions.v1.CloudFunctionsService.UpdateFunction","resourceName":"projects/smart-trader-cd29f/locations/us-central1/functions/getTickersAndSendRequestToStart"}
    macbookpro:functions cleo$ 

Don't know what to do, been searching for days and no results. Please help me find a way to be able to tell Firebase to install the private repository upon deployment of a function!

CodePudding user response:

After the rest of the day searching for the answer it was the most simple but not the wisest solution that worked:

Completely ignored the .npmrc file and in package.json under dependencies just added the personal access token like so: <GITHUB_PERSONAL_ACCESS_TOKEN>@github.com

"dependencies": {
    "@calugarul/ccxt.pro": "git https://<GITHUB_PERSONAL_ACCESS_TOKEN>@github.com/kroitor/ccxt.pro.git",
    "dotenv": "^10.0.0",
    "firebase-admin": "^9.8.0",
    "firebase-functions": "^3.16.0",
    "moment": "^2.29.1"
  },

It works and Cloud functions download the private repo but it's not the secure approach. If anybody has a more secure approach please let me know.

  • Related