Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

  • 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

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

I tried the same process with a folder gulp and 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?

like image 769
Nikola Avatar asked Dec 03 '14 09:12

Nikola


People also ask

How do I force an npm package to install?

The -f or --force argument will force npm to fetch remote resources even if a local copy exists on disk. The -g or --global argument will cause npm to install the package globally rather than locally.

Why npm install failed?

code 1 error usually occurs when you run the npm install command. This cause of this error is that one of the dependencies you define in your package. json file fails to be installed properly on your computer. This means that npm fails to install the node-sass module that's added as a dependency to the n-app project.

Does npm install install devDependencies?

NPM installs devDependencies within the package. json file. The 'npm install' command should add all the dependencies and devDependencies automatically during installation. If you need to add specific devDependencies to your project, you can use this command- 'npm install --save-dev'.


2 Answers

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.

like image 157
shennan Avatar answered Oct 07 '22 14:10

shennan


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.

like image 26
Kashif Nazar Avatar answered Oct 07 '22 13:10

Kashif Nazar