简体   繁体   English

如何使用 Firebase Cloud Functions.npmrc 设置私有 NPM 模块?

[英]How To Setup Private NPM Module With Firebase Cloud Functions .npmrc?

I have created a private typings npm module that I am using for my firebase functions and app projects.我创建了一个私有类型 npm 模块,用于我的 firebase 函数和应用程序项目。 When I went to deploy firebase functions, I get a big error for every function that basically says ERR: remote. Invalid username or password.当我去部署 firebase 函数时,每个 function 都会出现一个大错误,基本上是ERR: remote. Invalid username or password. ERR: remote. Invalid username or password.

For what I have read, it looks like I need to create a.npmrc file and put it in the /functions directory.对于我所阅读的内容,看起来我需要创建一个 .npmrc 文件并将其放在 /functions 目录中。 ( https://cloud.google.com/functions/docs/writing/specifying-dependencies-nodejs#using_private_modules ) ( https://cloud.google.com/functions/docs/writing/specifying-dependencies-nodejs#using_private_modules )

I cannot however find proper instructions on how to do this anywhere.但是,我无法在任何地方找到有关如何执行此操作的正确说明。 From what I found, I have done the following:根据我的发现,我做了以下事情:

  • ran npm login运行 npm 登录
  • ran npm token create --read-only跑了 npm token create --read-only

This then gave me a token that looks like this: XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX .然后这给了我一个看起来像这样的令牌: XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX

I then create a file called.npmrc in my functions directory, and placed //registry.npmjs.org/:_authToken=XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX in it.然后,我在我的函数目录中创建了一个名为 .npmrc 的文件,并将//registry.npmjs.org/:_authToken=XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX放入其中。

Additionally, I saw that the error message was trying to use ssh to install my private repo package, I have not setup ssh and am using https instead, because of this I changed my package file to git+https://github.com/accounts-name/repo#commit-num so that it uses HTTPS.此外,我看到错误消息是尝试使用 ssh 来安装我的私人仓库 package,我没有设置 ssh 而是使用 https,因此我将我的 package 文件更改为git+https://github.com/accounts-name/repo#commit-num以便它使用 HTTPS。

However, I still get the same error message.但是,我仍然收到相同的错误消息。 What am I missing?我错过了什么? The above is what I have strung together from lots of google searching.以上是我从大量谷歌搜索中串起来的。

It seems that you have made too many different changes while trying to make it work, so let's just go through the whole process step by step.看来您在尝试使其工作时进行了太多不同的更改,所以让我们逐步完成整个过程 go。

  1. Check the package.json of your npm module and publish it:查看你的npm模块的package.json并发布:

    • Remove "private" property or set it to false because private modules in the npm are meant to be never published.删除“私有”属性或将其设置为false ,因为 npm 中的私有模块永远不会发布。 That is not obvious but that is true.这并不明显,但确实如此。
    • Next step is to apply restricted access to the package. In order to do that, add such property in the package.json file:下一步是对 package 应用限制访问。为此,请在package.json文件中添加此类属性
     "publishConfig": { "access": "restricted" },
    • Make sure that npm account you use for publishing supports private packages.请确保您用于发布的npm帐户支持私有包。
    • Now open the terminal in the root directory of your package, type npm login then sign in to npm. Check if you put the proper version in the package.json .现在打开 package 根目录中的终端,键入npm login ,然后登录 npm。检查您是否将正确的版本放入package.json
    • Run npm publish .运行npm publish The package should be published in few seconds. package 应该会在几秒钟内发布。 No worries, thanks to publishConfig property nobody can access it.不用担心,由于publishConfig属性,没有人可以访问它。
  2. Now it is time to allow package installation in your project现在是时候允许在您的项目中安装 package

    • Go to the directory of the project and open package.json file Go 到工程目录下,打开package.json文件
    • Check that you have the name and version of your package in the dependencies list检查dependencies项列表中是否包含 package 的名称和版本
    • Open browser, navigate to https://npmjs.com , login to your account, navigate to settings page of your account and open the tokens tab打开浏览器,导航至https://npmjs.com ,登录您的帐户,导航至您帐户的设置页面并打开tokens选项卡
    • Create new token and copy it创建新令牌并复制它
    • Now again go to the directory of your project, on same level where package.json file is situated (that is important!) and create .npmrc file there.现在再次 go 到您的项目目录,与package.json文件所在的同一级别(这很重要!)并在那里创建.npmrc文件。
    • Put such string in the .npmrc file:将这样的字符串放在.npmrc文件中:
     //registry.npmjs.org/:_authToken=TOKEN_HERE

    You are done!你完成了!

  3. Deployment with remote CI/CD services使用远程 CI/CD 服务进行部署

    • The easiest approach is not add .npmrc into .gitignore .最简单的方法是不将.npmrc添加到.gitignore中。 In such case the file will be always in repository, so npm install will run smoothly on any machine where project was cloned在这种情况下,文件将始终在存储库中,因此npm install将在克隆项目的任何机器上顺利运行
    • If you don't want to have token string in the repository, you can move it to the environment variable of your CI/CD service and then link.npmrc file to that variable.如果你不想在存储库中有令牌字符串,你可以将它移动到你的 CI/CD 服务的环境变量,然后将 link.npmrc 文件移动到该变量。 For example, you can put generated token into the NPM_TOKEN env variable (Just token from npmjs, not the whole string from.npmrc!) And then change the .npmrc file in the next way: //registry.npmjs.org/:_authToken=${NPM_TOKEN} .例如,您可以将生成的令牌放入NPM_TOKEN env 变量(只是来自 npmjs 的令牌,而不是来自 .npmrc 的整个字符串!)然后按以下方式更改.npmrc文件://registry.npmjs.org/: //registry.npmjs.org/:_authToken=${NPM_TOKEN}

So, with those steps you should be able to install your restricted packages without any issues.因此,通过这些步骤,您应该能够毫无问题地安装受限软件包。 Good luck!祝你好运!

If you are trying to deploy you functions with firebase deploy from a CI and your .npmrc file looks like this.如果您尝试使用 firebase 从 CI 部署您的功能,您的.npmrc文件如下所示。

@acmecorp:registry=https://npm.pkg.github.com/

//npm.pkg.github.com/:_authToken=${NPM_REGISTRY_TOKEN}

You will run into the problem even if you have the env var set.即使您设置了环境变量,您也会遇到问题。

Build failed: Error: Failed to replace env in config: ${NPM_REGISTRY_TOKEN}

Firebase for some reason needs access to that private repo. Firebase 出于某种原因需要访问该私人仓库。 But the env var is not sent over to firebase.但是环境变量没有发送到 firebase。

Solution I've implemented was to replace ${NPM_REGISTRY_TOKEN} in the.npmrc file on every run of the CI pipeline.我实施的解决方案是在 CI 管道的每次运行中替换 .npmrc 文件中的${NPM_REGISTRY_TOKEN}

sed -i.bak "s/\${NPM_REGISTRY_TOKEN}/${NPM_REGISTRY_TOKEN}/g" .npmrc

This breaks if you use Yarn .如果您使用 Yarn ,这会中断。 Took me a while to find a thread pointing to npm install in the firebase cli predeploy step.我花了一段时间才在 firebase cli 预部署步骤中找到指向 npm 安装的线程。 If there's no package-lock.json and you only use yarn, this will break.如果没有 package-lock.json 并且你只使用纱线,这将会中断。 Remove yarn.lock and install using npm to resolve the issue.删除 yarn.lock 并使用 npm 安装以解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Firebase Cloud Functions 中安装 private GitHub npm package - Installing private GitHub npm package in Firebase Cloud Functions 如何为 firebase 云功能设置 vpc 连接器? - How to setup vpc connector for firebase cloud functions? 如何设置firebase触发邮件和云功能 - How to setup firebase trigger-mail and cloud functions 如何在 Firebase Cloud Functions 中对 npm 模块使用 `import`? - How do I use `import` for npm modules in Firebase Cloud Functions? Firebase Cloud Functions - npm 依赖项的“未定义”错误以及包含带有客户端代码的模块 - Firebase Cloud Functions - "not defined" error for npm dependency & including a module with client side code Firebase 云功能 - 自动认证设置 - Firebase cloud functions - automatic authentication setup 如何为 firebase 云函数导入基于模块的依赖项? - How to import module based dependencies for firebase cloud functions? 如何用npm导入firebase函数? - How to import firebase functions with npm? Firebase 在子文件夹中导入模块后,云功能未正确部署 - Firebase Cloud Functions did not deploy properly with imported a module in subfolder Cloud Functions 模拟器需要安装模块“firebase-admin” - The Cloud Functions emulator requires the module "firebase-admin" to be installed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM