Node Newbie Error – NPM Refusing to Install Package as a Dependency of Itself

I encountered an error and thought I’d post the solution here since it just might happen to you sometime!  Here’s the scenario:

I was planning to take the winston npm module out for a spin to try out it’s awesome logging capabilities.  I created a directory called winston to create a project and conduct the test.  What would be more logical than that?  🙂

Next, I ran npm init to create a package.json file hitting my Enter key as fast as it could go to accept all the defaults for this throwaway test project:

npm init screencast gif

It looks like we are good to go!  Let’s go ahead and install winston locally with the --save flag to save the dependency in the package.json file.

npm install winston --save

We are greeted with the following error:

npm ERR! Windows_NT 10.0.10586
npm ERR! argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Users\\Dave\\AppData\\Roaming\\npm\\node_modules\\npm\\bin\\npm-cli.js" "install" "winston"
npm ERR! node v5.3.0
npm ERR! npm  v3.6.0
npm ERR! code ENOSELF

npm ERR! Refusing to install winston as a dependency of itself
npm ERR! 
npm ERR! If you need help, you may report this error at:
npm ERR!     <https://github.com/npm/npm/issues>

npm ERR! Please include the following file with any support request:
npm ERR!     C:\!projects\node\winston\npm-debug.log

Uh oh, this is not looking good.  This looks like a serious error.  Winston is not able to install a dependency of itself.  What is the solution?

Let’s inspect the package.json file:

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

On line 2, we see that the name of our project is winston.  I don’t think npm likes that since we are trying to install a package named “winston”.  Let’s rename our project to “winston-test”, and save the package.json file:

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

Let’s now try to install the npm package again:

npm install winston --save

This time, the npm package installs successfully:

[email protected] C:\!projects\node\winston
`-- [email protected]
  +-- [email protected]
  +-- [email protected]
  +-- [email protected]
  +-- [email protected]
  +-- [email protected]
  +-- [email protected]
  `-- [email protected]

npm WARN [email protected] No description
npm WARN [email protected] No repository field.

You will see that npm does give us a couple of warning messages, but these are nothing we need to worry about for testing and learning purposes.  We are back in business!

Share

81 thoughts on “Node Newbie Error – NPM Refusing to Install Package as a Dependency of Itself

  1. Very Useful. You might want to name the name of your project the same as the module you want to install which make total sense especially if you want to reuse the code. Thank you for the tip

    1. You are very welcome, IsmaelKP. Thanks for taking the time to provide feedback and let me know that this article was useful to you!

  2. As a beginner developer I really appreciate this information. I wouldn’t have any chance to figure out this problem by myself.

  3. Refusing to install angular as a dependency of itself [SOLVED] 🙂
    When I tried to install Angular 1 to Meteor, the same error was occurring. Thank you very much for your solution!!! 🙂

  4. You saved a lot of inanimate objects in my house tonight. Give me a PayPal account and your next round of drinks is on me.

    Thanks!

  5. How to check if npm install has done its installation and where will be the node_modules located for npm in centos user machine?

  6. more than a year on … working away on my laptop late night ; mighty kicked with years of node development and I come across this error. Dumbstruck would be an understatement. I see your post and its a RELIEF, because of all the things (lets not get started) that you need to investigate … who would look at the package.json ??! Thank YOU

    1. Vijay, thanks for sharing your story about how my article was able to help you! I was also dumbstruck when I encountered this issue, and I’m glad I took the time to document the solution since it appears to be something that provides an element of surprise/frustration for others too. 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *