[英]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:
根据我的发现,我做了以下事情:
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。
Check the package.json of your npm module and publish it:查看你的npm模块的package.json并发布:
false
because private modules in the npm are meant to be never published.false
,因为 npm 中的私有模块永远不会发布。 That is not obvious but that is true.package.json
file:package.json
文件中添加此类属性: "publishConfig": { "access": "restricted" },
npm
account you use for publishing supports private packages.npm
帐户支持私有包。npm login
then sign in to npm. Check if you put the proper version in the package.json
.npm login
,然后登录 npm。检查您是否将正确的版本放入package.json
。npm publish
.npm publish
。 The package should be published in few seconds. publishConfig
property nobody can access it.publishConfig
属性,没有人可以访问它。 Now it is time to allow package installation in your project现在是时候允许在您的项目中安装 package
package.json
file package.json
文件dependencies
listdependencies
项列表中是否包含 package 的名称和版本tokens
tabtokens
选项卡package.json
file is situated (that is important!) and create .npmrc
file there.package.json
文件所在的同一级别(这很重要!)并在那里创建.npmrc
文件。.npmrc
file:.npmrc
文件中: //registry.npmjs.org/:_authToken=TOKEN_HERE
You are done!你完成了!
Deployment with remote CI/CD services使用远程 CI/CD 服务进行部署
.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 clonednpm install
将在克隆项目的任何机器上顺利运行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.