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:

winston-test@1.0.0 C:\!projects\node\winston
`-- winston@2.1.1
  +-- async@1.0.0
  +-- colors@1.0.3
  +-- cycle@1.0.3
  +-- eyes@0.1.8
  +-- isstream@0.1.2
  +-- pkginfo@0.3.1
  `-- stack-trace@0.0.9

npm WARN winston-test@1.0.0 No description
npm WARN winston-test@1.0.0 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!

22 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!

Leave a Reply

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