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

后端 未结 5 1147
傲寒
傲寒 2020-12-14 14:35

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

  • mkdir hapi && cd hapi
  • npm init
  • npm
相关标签:
5条回答
  • 2020-12-14 15:05

    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.

    0 讨论(0)
  • 2020-12-14 15:11

    Reason Module name is same with library name

    Solution

    1. Change the module name to something else
    2. Change 'name' in package.json
    0 讨论(0)
  • 2020-12-14 15:13

    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.

    So it's not the name of the folder that is stopping you from installing the dependency, it is the name of the npm module that you are creating.

    Open the resulting package.json within your hapi directory, 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:

    {
      "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 CommonJS philosophy.

    0 讨论(0)
  • 2020-12-14 15:17

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

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

    0 讨论(0)
  • 2020-12-14 15:23

    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 react@1.0.0 No description
    npm WARN package.json react@1.0.0 No repository field.
    npm WARN package.json react@1.0.0 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

    {
      "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

    0 讨论(0)
提交回复
热议问题