在 Firebase Cloud Functions 中安装私有 GitHub npm 包

Installing private GitHub npm package in Firebase Cloud Functions

我是 Firebase Cloud Functions 的新手,一直在努力添加私有 npm 包以使我的函数正常工作。我知道 Firebase 会将它们全部视为 public 除非指定,并且将使用 npm / yarn 安装我在 package.json.

中的内容

我告诉 Firebase 它是 Github 上的私有存储库的唯一方法是添加

.npmrc(包含)- 我使用的密钥是来自 Github 的个人访问令牌-具有所有需要它的权限的开发人员

//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

在我的 index.js 文件中,我调用了私有存储库 (ccxt.pro),但它不会安装在 Firebase Cloud Functions 服务器上。

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(包含)

{
  "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
}

常见错误:

 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$ 

不知道怎么办,找了好几天也没有结果。请帮我找到一种方法,让 Firebase 在部署功能时安装私有存储库!

在一天剩下的时间里寻找答案后,这是最简单但不是最明智的解决方案:

完全忽略 .npmrc 文件并在 package.json 依赖项下添加个人访问令牌,如下所示:@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"
  },

它有效并且云功能下载私有存储库,但这不是安全的方法。如果有人有更安全的方法请告诉我。

如果您使用 yarn (yarn.lock) 而没有 package-lock.json,它会中断,因为 firebase CLI 在 pre-deploy 中使用 npm install。删除 yarn.lock 并使用 npm install.