node.js npm WARN install 拒绝安装 hapi 作为自身的依赖

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27267707/
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-02 18:11:24  来源:igfitidea点击:

npm WARN install Refusing to install hapi as a dependency of itself

node.jsnode-modules

提问by Nikola

I tried to do the following (per instructions from official site):

我尝试执行以下操作(根据官方网站的说明):

  • mkdir hapi && cd hapi
  • npm init
  • npm install hapi --save
  • mkdir hapi && cd hapi
  • npm init
  • npm install hapi --save

But this gives me an error like this:

但这给了我这样的错误:

npm WARN install Refusing to install hapi as a dependency of itself

npm WARN install 拒绝安装 hapi 作为自身的依赖

Now, I made a new test folder called hapiTestand repeated the commands and then everything worked fine.

现在,我创建了一个名为的新测试文件夹hapiTest并重复这些命令,然后一切正常。

I tried the same process with a folder gulpand npm install gulp --save, and got the same error, so my conclusion is that I can't have the name of the folder be the same as the package that I want to install, but can someone back this statement up with some official documentation?

我对文件夹gulp和尝试了相同的过程npm install gulp --save,并得到了同样的错误,所以我的结论是我不能让文件夹的名称与我要安装的包相同,但有人可以支持这个声明有一些官方文件吗?

回答by shennan

When you did the command npm init, there were probably some relevant questions you needed to answer. Specifically, the name of your module. When you use npm init, it assumes you want the name of the module you're creating to be called the name of the folder it is in.

当您执行命令时npm init,您可能需要回答一些相关问题。具体来说,就是您的模块的名称。当您使用 时npm init,它假定您希望正在创建的模块的名称被称为它所在文件夹的名称。

So it's not the name of the folderthat is stopping you from installing the dependency, it is the name of the npm modulethat you are creating.

因此,阻止您安装依赖项的不是文件夹的名称,而是您正在创建的npm 模块的名称。

Open the resulting package.jsonwithin your hapidirectory, and rename the module to something other than hapi. Here's an example 'package.json' that works, even when residing in a folder called hapi:

打开生成package.json的内部hapi目录,并重新命名模块比其他的东西hapi。这是一个有效的示例“package.json”,即使驻留在名为hapi的文件夹中:

{
  "name": "hapi-test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "hapi": "^7.5.2"
  }
}

Added Note

添加注释

I've not been able to find any documentation thus-far explaining this phenomena in the context of npm; though it is a bit of a no-brainer. Requiring modules with the same name within the same application would conflict with the CommonJSphilosophy.

到目前为止,我还没有找到任何文档来解释 npm 上下文中的这种现象;虽然这有点不费吹灰之力。在同一个应用程序中要求具有相同名称的模块会与CommonJS哲学相冲突。

回答by Kashif Nazar

The name of your module is same as the module you are trying to install. NPM thinks that you are installing the module to itself. Change the name of your module and it will install perfectly.

您的模块名称与您尝试安装的模块名称相同。NPM 认为您正在为自己安装模块。更改模块的名称,它将完美安装。

回答by Shanky Munjal

ReasonModule name is same with library name

原因模块名称与库名称相同

Solution

解决方案

  1. Change the module name to something else
  2. Change 'name' in package.json
  1. 将模块名称更改为其他名称
  2. 更改 package.json 中的“名称”

回答by samuelj90

The issue can be simply explained as follows the name of your package or module in package.jsoncannot be same as that of the package or module you are trying to install.

该问题可以简单地解释如下 :您的包或模块的名称package.json不能与您尝试安装的包或模块的名称相同

Here hapiis the name of your module and you are trying to install a module with name hapiwith npm install hapi --save

hapi是您的模块的名称,您正在尝试安装名称hapinpm install hapi --save

回答by cryptoKTM

This was my initial code

这是我最初的代码

{
  "name": "react",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "react": "^15.6.1"
  }
}

which threw error

哪个抛出错误

npm WARN package.json [email protected] No description
npm WARN package.json [email protected] No repository field.
npm WARN package.json [email protected] No README data
npm WARN install Refusing to install react as a dependency of itself

then i renamed the name from react to react_app and my code looks like

然后我将名称从 react 重命名为 react_app,我的代码看起来像

{
  "name": "react_app",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "react": "^15.6.1"
  }
}

then it worked

然后它起作用了