“npm config set registry https://registry.npmjs.org/”在 Windows bat 文件中不起作用

Posted

技术标签:

【中文标题】“npm config set registry https://registry.npmjs.org/”在 Windows bat 文件中不起作用【英文标题】:"npm config set registry https://registry.npmjs.org/" is not working in windows bat file 【发布时间】:2014-04-18 13:39:37 【问题描述】:

我在windows 7上创建a.bat,a.bat的内容是:

@echo off
npm config set registry https://registry.npmjs.org/

然后运行a.bat,但是不行,我发现“set”这个词是npm和bat的特殊关键字,有什么方法可以解决这个问题吗?

【问题讨论】:

是否有其他人每次想查看 npm 注册表 url 时都会来此帖子?哈哈 仅供参考:我需要 .com 版本的注册表:即 https://registry.npmjs.com/ 而不是 https://registry.npmjs.org/ @kevingilbert100 npm config get registry 了解注册表 URL @Sridhar 不。这只提供当前设置的注册表,而不是“官方”npm 注册表。 必须从 https 更改为 http 【参考方案1】:

您不应使用 .bat 文件更改 npm 注册表。 而是尝试使用修改.npmrc 文件,该文件是npm 的配置。 更改注册表的正确命令是

npm config set registry <registry url>

您可以使用npm help config 命令找到更多信息,还可以在以这种方式运行.bat 文件时检查权限。

【讨论】:

可能是我用的方法不适合我的问题,不过还是谢谢你的回答:) @MarcellodeSales 尝试npm config set registry=<registry url> config 部分不是必需的,只需运行npm get registry 即可查看您的当前状态,运行npm set registry https://registry.npmjs.org/ 可将其设置回默认值。 是 npm config set registry 可能的。如果我不想从互联网上下载任何东西并且所有工件都在应用程序中。【参考方案2】:

我们还可以为多个自定义注册表 URL 运行带有 registry 选项的 npm install。

npm install --registry=https://registry.npmjs.org/ 
npm install --registry=https://custom.npm.registry.com/ 

【讨论】:

运行 npm install.npmrc 设置为私人工件造成了很多痛苦。这为我节省了几个小时??【参考方案3】:

您可以使用 .bat 进行更改,确保您事先运行了 call 命令,希望这有助于将来制作类似 .bat 命令的任何人

call npm config set registry https://registry.npmjs.org/

【讨论】:

@prayagupd 什么??【参考方案4】:

在 4.4.1 版本上,您可以使用:

npm config set @myco:registry=http://reg.example.com

@myco 是您的包范围。你可以这样安装包:

npm install @myco/my-package

参考:https://docs.npmjs.com/misc/scope

【讨论】:

【参考方案5】:

我可能来不及回答了。但是如果有人需要它,following 可以正常工作,因为我已经使用过很多次了。

npm config set registry=https://registry.npmjs.com/

【讨论】:

【参考方案6】:

    全局设置 npm 注册表

    使用以下命令为登录用户修改 .npmrc 配置文件

    npm config set registry <registry url>

    例子: npm config set registry https://registry.npmjs.org/


    设置 npm 注册表作用域

    范围允许将相关包组合在一起。范围包将安装在 node_modules 文件夹下的子文件夹中。

    示例node_modules/@my-org/packagaename

    要设置范围注册表使用:npm config set @my-org:registry http://example.reg-org.com

    要使用范围安装软件包,请使用:npm install @my-org/mypackage

    每当您从 @my-org 范围安装任何软件包时,npm 都会在链接到范围 @my-org 的注册表设置中搜索注册表 url。


    为项目在本地设置 npm 注册表

    仅为当前项目修改 npm 注册表。在项目的根文件夹中创建一个文件为.npmrc

    在文件中添加以下内容

   registry = 'https://registry.npmjs.org/'

【讨论】:

谢谢!我试过npm config set registry https://...,但是当我做npm config get registry时它返回http://...,问题是有人用http://...提交了.nmprc,它优先于全局配置。我花了差不多 10 分钟,直到我明白在你的帖子的帮助下会发生什么:)【参考方案7】:

在 npm 版本 3.7.3 上

npm set registry=http://whatever/

【讨论】:

【参考方案8】:

通过执行您的 .bat,您只是为该会话而不是全局设置配置。当您打开另一个 cmd 提示符并运行 npm install 时,不会为该会话设置该配置,因此将您的 .bat 文件修改为

@echo off
npm config set registry https://registry.npmjs.org/
@cmd.exe /K

【讨论】:

【参考方案9】:
2.name can no longer contain capital letters

不要在包裹中使用大写字母:

npm install --save uex

使用这个:

npm install --save vuex

【讨论】:

【参考方案10】:

正如Gntem 指出的那样,您可能无法使用.bat 文件更改npm 注册表。 但我知道您需要能够自动更改注册表。 您可以通过将您的 .npmrc 配置放在单独的文件中(例如 npmrc_jfrognpmrc_default)并让您的 .bat 文件执行复制任务来做到这一点。

例如(在 Windows 中): 您的 default_registry.bat 将拥有

xcopy /y npmrc_default .npmrc

你的 jfrog_registry.bat 将拥有

xcopy /y npmrc_jfrog .npmrc

注意:/y 禁止提示确认您要覆盖现有目标文件。

这将确保所有配置属性(registry、proxy、apiKeys 等)都被复制到.npmrc

您可以阅读有关 xcopy here 的更多信息。

【讨论】:

以上是关于“npm config set registry https://registry.npmjs.org/”在 Windows bat 文件中不起作用的主要内容,如果未能解决你的问题,请参考以下文章