npm adduser 通过 bash

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24143973/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 10:38:47  来源:igfitidea点击:

npm adduser via bash

bashcommand-linenpm

提问by ke_wa

I want to automate the npm login process via a bash script.

我想通过 bash 脚本自动化 npm 登录过程。

I tried it with this snippet:

我用这个片段试了一下:

/usr/bin/expect -f - <<EOD
spawn npm adduser
expect "Username:"
send "myUserName\n"
expect "mail: (this IS public)"
send "[email protected]\n"
EOD

But without luck.

但没有运气。

Note: I will change the strings with env variables

注意:我将使用 env 变量更改字符串

回答by ke_wa

@Aurélien Thieriot: thanks for the hint.

@Aurélien Thieriot:感谢您的提示。

I have two solutions for my problem:

我的问题有两个解决方案:

Solution 1

方案一

export NPM_AUTH_TOKEN=myToken
export NPM_EMAIL=myEmail

create/override ~/.npmrcby following shell script:

~/.npmrc通过以下 shell 脚本创建/覆盖:

echo "_auth = $NPM_AUTH_TOKEN" > ~/.npmrc
echo "email = $NPM_EMAIL" >> ~/.npmrc

Solution 2

解决方案2

export NPM_USERNAME=myUsername
export NPM_PASSWORD=myPassword
export NPM_EMAIL=myEmail

I know the order of the questions. So I can do the following:

我知道问题的顺序。所以我可以执行以下操作:

npm adduser <<!
$NPM_USERNAME
$NPM_PASSWORD
$NPM_EMAIL
!

Note: solution 2 works only when the user isn't added yet
Otherwise the $NPM_PASSWORDis not necessary

注意:解决方案 2 仅在尚未添加用户时才有效,
否则$NPM_PASSWORD没有必要

回答by Seralto

This way works and with a more elegant expect:

这种方式有效并且具有更优雅的期望

/usr/bin/expect <<EOD
spawn npm adduser
expect {
  "Username:" {send "$USERNAME\r"; exp_continue}
  "Password:" {send "$PASSWORD\r"; exp_continue}
  "Email: (this IS public)" {send "$EMAIL\r"; exp_continue}
}
EOD

回答by Adam K Dean

I had this issue but the only way of getting round it was to wrap expect into a docker image. You can use it like so:

我遇到了这个问题,但解决它的唯一方法是将 expect 包装到 docker 映像中。你可以像这样使用它:

docker run \
    -e NPM_USER=$NPM_USER \
    -e NPM_PASS=$NPM_PASS \
    -e NPM_EMAIL=$NPM_EMAIL \
    bravissimolabs/generate-npm-authtoken \
    > ~/.npmrc

https://github.com/bravissimolabs/docker-generate-npm-authtoken

https://github.com/bravissimolabs/docker-generate-npm-authtoken

回答by Iain Ballard

I found that on Windows Server 2012R2, there is some odd behaviour with service accounts. This method worked for me (as part of a Jenkins build, under bash):

我发现在 Windows Server 2012R2 上,服务帐户存在一些奇怪的行为。这种方法对我有用(作为 Jenkins 构建的一部分,在 bash 下):

cat > ~/.npmrc <<EOL
//my.local.registry:4873/:_authToken="G....................A=="
always_auth=true
registry=http://my.local.registry:4873/
user=aRegisteredUser
EOL

回答by Aurélien Thieriot

I don't know if it is in any way secured so please do some research before.

我不知道它是否以任何方式受到保护,所以请先做一些研究。

But the fact is that npmis storing all those informations into a file. If you look at:

但事实是npm将所有这些信息存储到一个文件中。如果你看:

cat ~/.npmrc

It could be interesting enough so you could do the login dance only once.

它可能足够有趣,因此您只能进行一次登录舞蹈。

回答by Salvatore Napoli

My Solution is to use plugin npm-login-cmd

我的解决方案是使用插件 npm-login-cmd

npm install -g npm-login-cmd

export NPM_USER=user
export NPM_PASS=pass
export NPM_EMAIL=valid email syntax
npx npm-login-cmd

login work on enterprice npm repository

在 enterprice npm 存储库上登录工作

回答by Pierre Maoui

For people working with a private registry (typically for CI purpose), reaching directly the Rest API may be a solution :

对于使用私有注册表的人(通常用于 CI 目的),直接访问 Rest API 可能是一个解决方案:

curl -XPUT -H "Content-type: application/json" -d '{ "name": "your_username", "password": "your_password" }' 'http://localhost:4873/-/user/org.couchdb.user:your_username'

This is what npm adduseris doing behind the scene.

这就是npm adduser幕后所做的事情。

回答by David Boyd

Didn't have luck with any answers above on OSX.

在 OSX 上没有得到上述任何答案的运气。

This did work though:

不过这确实有效:

npm install -g npm-cli-adduser
npm-cli-adduser -u username -p password -e email -r https://repo.com/nexus

回答by uzay95

using with npm-cli-login package it worked

与 npm-cli-login 包一起使用它有效

# npm install -g npm-cli-login
# npm-cli-login -u myUser -p myPass -e [email protected] -r http://192.168.56.1:4873

Checking if it is installed or not:

检查是否安装:

# whereis npm-cli-login
npm-cli-login:
# whereis npm-cli-login | grep '/npm-cli-login' -ic
0

After installation:

安装后:

# npm install -g npm-cli-login

Check if it is installed:

检查是否安装:

# whereis npm-cli-login
npm-cli-login: /usr/bin/npm-cli-login
# whereis npm-cli-login | grep '/npm-cli-login' -ic
1

Let's login:

让我们登录:

# npm-cli-login -u myUser -p myPass -e [email protected] -r http://192.168.56.1:4873
info attempt registry request try #1 at 10:13:19 PM
http request PUT http://192.168.56.1:4873/-/user/org.couchdb.user:myUser
http 409 http://192.168.56.1:4873/-/user/org.couchdb.user:myUser
info attempt registry request try #1 at 10:13:19 PM
http request GET http://192.168.56.1:4873/-/user/org.couchdb.user:myUser?write=true
http 200 http://192.168.56.1:4873/-/user/org.couchdb.user:myUser?write=true
info attempt registry request try #1 at 10:13:20 PM
http request PUT http://192.168.56.1:4873/-/user/org.couchdb.user:myUser/-rev/undefined
http 201 http://192.168.56.1:4873/-/user/org.couchdb.user:myUser/-rev/undefined
#
# npm whoami
myUser
#
# npm logout
# npm whoami
npm ERR! code ENEEDAUTH
npm ERR! need auth This command requires you to be logged in.
npm ERR! need auth You need to authorize this machine using `npm adduser`

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2020-04-21T22_13_42_373Z-debug.log