188

This is my package.json for the module that I'm including in the parent project:

{
  "version": "0.0.1",
  "name": "module-name",
  "dependencies": {
    "express": "3.3.4",
    "grunt": "0.4.1",
    "grunt-contrib-compass": "0.4.0",
    "grunt-contrib-copy": "0.4.1",
    "grunt-contrib-cssmin": "0.4.1",
    "grunt-contrib-jshint": "0.6.3",
    "grunt-contrib-requirejs": "0.4.1",
    "grunt-contrib-uglify": "0.2.2",
    "grunt-contrib-watch": "0.5.1",
    "grunt-express-server": "0.4.1",
    "grunt-karma": "0.4.5",
    "grunt-regex-replace": "0.2.5",
    "request": "2.25.0"
  },
  "scripts": {
    "postinstall": "grunt install"
  }
}

One thing to note is that this module is contained in a private repo and I include it in the parent package.json like: "module-name": "git+ssh://git@myserver:user/module-name.git"

allprog
  • 16,540
  • 9
  • 56
  • 97
Ahmed Nuaman
  • 12,662
  • 15
  • 55
  • 87
  • 1
    Your package.json looks ok, as does the line you use to include it. Do you get an error message? Is git on your path? – poida Aug 23 '13 at 11:34
  • 1
    I get no error per sa, the module installs but my `postinstall` script fails because local `grunt` packages are not found as they're not installed by NPM – Ahmed Nuaman Aug 23 '13 at 11:45
  • 1
    This problem still exists in November 2016. I had done a clean install of a working module on a new computer. @Mohsen's answer fixed it. – Joe Lapp Nov 11 '16 at 04:04
  • What I've tried and Helped: rimraf node_modules npm cache clean --force npm i The most important: Close the VS Code and re-open it. Only after I did that all the errors were gone. Good luck! – Uri Gross Aug 30 '22 at 15:27

21 Answers21

179

It looks like you hit a bug that has existed for quite a while and doesn't have solution yet. There are several open issues for this case in the npm repository:

In the first one people list several workarounds that you may try.

An alternative solution may be (a little hackish) to explicitly list the dependencies as first level dependents. This requires you to maintain the list but practically it has to be done very infrequently.

Gordon Leigh
  • 1,263
  • 2
  • 11
  • 23
allprog
  • 16,540
  • 9
  • 56
  • 97
  • 1
    @Flame2057 it's good to know you're out of luck, though not too informative. You may go ahead and formulate a question, hopefully the community has the knowledge to solve. Have fun! – allprog Jul 17 '17 at 14:17
  • 1
    One additional step was needed for me. I had to right-click on "Dependencies" in my project and choose "Restore Packages" after doing other steps mentioned here (delete node_modules and package.lock.json, then npm install). – Taersious Mar 19 '18 at 15:56
148

I had very similar issue, removing entire node_modules folder and re-installing worked for me. Learned this trick from the IT Crowd show!

rm -rf node_modules
npm install
Mohsen
  • 64,437
  • 34
  • 159
  • 186
  • 1
    This actually worked for me. I think that is really weird - what is causing that glitch? Regardless, if I delete the folder and re-run npm install, then It works. Beforehand, nothing would install in that subdirectory. Now, it all gets installed there. – CtheGood May 12 '15 at 23:06
  • Thanks @Moshen. I had removed an old version of Node and did a clean install. I removed all my node_modules/ globally but forgot to remove then from some existing apps. I had to delete the node_modules folder in each existing app's directory, then run `npm install`. – wilblack Jan 31 '16 at 19:02
  • The good old "did you try restarting from _scratch_ approach?". This worked great for me. – Marco Aurélio Deleu Aug 03 '16 at 22:09
  • While it is nice to know there is a workaround, this really smacks of 'reboot your machine and try again' and just hides the real problem. I have a similar problem with NPM install (process exits immediately with no message) and one of the workarounds is to delete the node_modules folder and reinstall; which does not fill me with confidence that we understand what is going on here. – Kelly S. French Sep 19 '16 at 14:25
  • 1
    You sir, are a Legend – SrAxi Jun 16 '17 at 15:18
  • Happened to me too, when trying npm update (after some issue with my vagrant box falling in invalid state; probably not related... but who knows...). Usually it never gives any problem. Removing/renaming *node_modules* did the trick. Maybe explicitly clearing the cache (that should not be needed with the latest NPM versions) could help too. **Note:** after that I restored the old/renamed *node_modules* and re-tried install: and this time it worked (suggests to me that the issue might not be in the directory itself). My 2 cents. – Kamafeather Jul 24 '18 at 22:38
  • 2
    This worked for me. I'f I could +1 again for the IT Crowd reference I would. – texelate Nov 05 '18 at 14:01
  • Which episode? if I'm allowed to ask lol – milkman15 Apr 10 '22 at 22:10
  • Also incase you are having the link error issue, just use `yarn` after removing all the node modules, to install again – Damola Obaleke Jul 19 '22 at 01:36
66

I am using windows machine.

  1. I deleted node_modules folder.
  2. Somehow, package.lock.json file is getting created. I deleted that file.
  3. Then npm install.
  4. Clean build.
  5. Run.
Dmitry
  • 6,716
  • 14
  • 37
  • 39
Virat18
  • 3,427
  • 2
  • 23
  • 29
  • 29
    This answer needs to get more love. The package.lock.json file was screwing me. – mwilcox Sep 12 '17 at 16:39
  • Deleting package.lock.json did it for me too. Keeping it would lead to the same behaviour (not installing dependencies). – Jan Jan 19 '22 at 17:39
  • 1
    I'm using a monorepo, I also had to remove any sub node_modules, not just the root level! Hope this helps – AlexMorley-Finch May 22 '23 at 10:33
17

if you inherited this code, it could be that the dependencies and versions were locked and you have a ./npm-shrinkwrap.json file.

if your dependency is not listed in that file, it will never get installed with the npm install command.

you will need to manually install the packages and then run npm shrinkwrap to update the shrinkwrap file.

  • 2
    I had an issue where manually installing a package had created a `package-lock.json` file after updating to node 8.0.0 and npm 5.0.0. After that, running `npm install` would only ever install that one package instead of all off my dependencies. Deleting `package-lock.json` and re-running `npm install` solved the issue for me. – Kyle Pittman Jun 01 '17 at 15:53
  • 1
    @Monkpit That solved it for me as well, thanks! `package-lock.json` was getting in the way. – VirtualWolf Jun 13 '17 at 02:48
16

Also check that your package name is correctly accepted:

WRONG:

{
    "name": "My Awesome Package"
}

CORRECT:
{
    "name": "my-awesome-package-name"
}
Caius
  • 2,084
  • 6
  • 31
  • 47
  • 1
    Yeah I had a space in the name and it didn't install properly. – Ansjovis86 Apr 27 '20 at 13:17
  • 1
    not only for space, it's also about the '-' he used in the name, i have also faced the same issue, put - or _ in the name, it will work. – molagbal Jun 05 '20 at 04:42
  • Can you edit to add a space after the `:` in the WRONG example, because I believe that particular space doesn't matter, yet it's different from the one below -- so it's distracting. – Wyck Aug 23 '21 at 18:55
  • 1
    this kind of error throws an error immediately after npm install and is not the cause of op issue – JRichardsz Mar 28 '22 at 21:44
  • @JRichardsz maybe now, but look at the date of the answer... – Caius Mar 29 '22 at 23:02
13

In my case it helped to remove node_modules and package-lock.json.

After that just reinstall everything with npm install.

Lars Kliesing
  • 131
  • 1
  • 5
12

OP may be true for an older version of node. However, I faced the same with node 4.4.1 as well.

It very well may be linked to the node version you are using. Try to upgrade to a latest version. Certain dependencies don't load transitively if they are incompatible with node version.

I found this by running npm update.

After upgrading to latest version (4.4 -> 5.9); this got fixed.

Shaun Luttin
  • 133,272
  • 81
  • 405
  • 467
Nrj
  • 6,723
  • 7
  • 46
  • 58
9

I suspect you're facing the issue where your package.json file is not in the same directory as your Gruntfile.js. When you run your grunt xxx commands, you get error an message like:

Local Npm module "xxx" not found. Is it installed?

For now, the solution is:

  • Create package.json in the same directory as Gruntfile.js
  • Define the modules required by your grunt project
  • Execute npm install to load them locally
  • Now the required grunt command should work.

IMHO, it is sad that we cannot have grunt resolve modules loaded from a parent npm module (i.e. package.json in a parent directory within the same project). The discussion here seems to indicate that it was done to avoid loading "global" modules but I think what we want is loading from "my project" modules instead.

kctang
  • 10,894
  • 8
  • 44
  • 63
  • So this is the structure of my parent project (that's the one importing the sub module project): http://cl.ly/image/020E0b3I0t1A and the structure of my sub module project: http://cl.ly/image/2q0L3l1v0s40; you can see that both contain `Gruntfile.js` and both contain the `package.json` on the same level. – Ahmed Nuaman Aug 31 '13 at 18:38
  • 1
    This isn't an issue with `grunt`, it's an issue with `npm` not recursively installing the dependancies in my sub module project. – Ahmed Nuaman Aug 31 '13 at 18:39
5

Worth to mention to make sure your dependencies should be in the dependencies part of your package.json (as opposed to devDependencies).

My issue was basically the same as OP:

  • installing a private repo (Let's call it repo1) via "module-name": "git+ssh://git@myserver:user/my-repo-name.git" in other repo(Let's call it repo2),
  • in repo2's node_modules, one package dependency from repo1 wasn't there.
  • My silly mistake!.. repo1 was listing that dependency in devDependencies instead of dependencies
  • Move the dependency in my repo1's package.json from devDependencies to dependencies
  • In my repo2, I removed my node_modules and package-lock.json, did npm install, an voilà!... dependency was there!
cafesanu
  • 435
  • 1
  • 4
  • 12
  • I had declared the NODE_ENV=production environment variable in my Dockerfile above the npm install command, which causes devDependencies not to be installed – Pieter De Clercq Aug 02 '19 at 15:16
4

You might need to install the grunt-cli, try this before doing a npm install:

sudo npm install -g grunt-cli

That fixes the grunt does not exit for me, you'll also need a valid grunt file.

Source: https://stackoverflow.com/a/16456467/241294

Community
  • 1
  • 1
poida
  • 3,403
  • 26
  • 26
  • What happens when you try the command 'grunt' at your command prompt? Does it exist on your path? You're right, it does look like it's installed from your pastebin. Did you install it with sudo and -g? – poida Aug 23 '13 at 13:06
  • It's not an issue with grunt: `~ ᐅ which grunt /opt/local/bin/grunt` – Ahmed Nuaman Aug 23 '13 at 13:24
  • Also when I clone the repo itself, rather than via NPM and run `npm install` and `grunt install`, it works fine. The issue is that NPM doesn't seem to install the repo's dependancies as specified in the `package.json` – Ahmed Nuaman Aug 23 '13 at 13:25
  • In which directory are you looking for the packages dependencies? They shouldn't be at the top level but under: top-level-package / node_modules / module-name / node_modules. Could it be that grunt isn't finding the grunt file because it's working in the wrong directory? – poida Aug 23 '13 at 13:29
  • It's not an issue with `grunt`, when you install modules with NPM it usually provides a summery at the end of any dependancies of that module that have already been installed; when I install the module in question I don't receive a summery of any dependency modules installed. – Ahmed Nuaman Aug 23 '13 at 14:27
4

Just in case anyone is suffering from this predicament and happens to make the same asanine mistake that I did, here is what it was in my case. After banging my head against the wall for an hour, I realized that I had my json incorrectly nested, and the key "dependencies" was inside of the key "repository".
Needless to say, no errors were evident, and no modules were installed.

dgo
  • 3,877
  • 5
  • 34
  • 47
4

Another way to work this around is to add this into your module package.json scripts section

"preinstall": "npm install {Packages You depend on}"

what this will does is, it will install all packages needed by the module and you won't get that error.

Ahmed Fathy
  • 166
  • 1
  • 6
3

happens with old node version. use latest version of node like this:

$ nvm use 8.0
$ rm -rf node_modules
$ npm install
$ npm i somemodule

edit: also make sure you save.
eg: npm install yourmoduleName --save

Deke
  • 4,451
  • 4
  • 44
  • 65
2

I was receiving this error when I installed a clean Node dev environment on windows.

To fix this, I went into my new project directory (that I just scaffolded with yo angular) and typed in two commands:

npm install -g grunt --save-dev

That will install the local grunt dependencies to your project. Next:

npm install

That will ensure all your (new) project dependencies are installed.

Tada!

Jesse Lawson
  • 715
  • 8
  • 13
2

What I've tried and Helped:

rimraf node_modules  
npm cache clean --force 
npm i

The most important: Close the VS Code and re-open it. Only after I did that all the errors were gone.

Good luck!

Uri Gross
  • 484
  • 1
  • 8
  • 20
1

I faced the issue when I was trying to create the first Angular application.

ng new angular-tour-of-heroes
cd angular-tour-of-heroes
ng serve --open

So every time I tried to compile the app I get the dependency error. Finally was able to resolve the issue after running

npm install -g @angular/cli@latest

After that ng serve command works without any error when I replicate the above steps for new project.

0

I had the same problem. But on the same machine one project had good package.json, where all my dependencies are successfully installed. And in another project my package.json dependencies were not installed no matter what i do. I just copied the package.json and pasted into that another project. And it worked! The difference i have found was only empty line at the start of file. Dont know or it influences anything, maybe some other problem. But the problem was only the package.json file.

mansim
  • 727
  • 7
  • 21
0

I think that I also faced this problem, and the best solution I found was to look at my console and figure out the error that was being thrown. So, I read it carefully and found that the problem was that I didn't specify my repo, description, and valid name in my package.json. I added those pieces of information and everything was okay.

ejderuby
  • 710
  • 5
  • 21
Vainqueur
  • 21
  • 5
0

I've been hours trying to debug this issue and finally it was that I had the NODE_ENV= env variable set to "production".

From https://riptutorial.com/node-js/example/10101/setting-node-env--production- "When the NODE_ENV environment variable is set to 'production' all devDependencies in your package.json file will be completely ignored when running npm install."

Abdallah
  • 402
  • 4
  • 14
0

fix

As @Monkpit said : Deleting package-lock.json and re-running npm install solved the issue for me. But don't forget to push the new package-lock.json*.

explanation

Don't download the inherit dependencies would be an awful issue and Nodejs don't have it.

The error is caused by an older package-lock.json caused for the use of libraries not hosted in the official repository : https://www.npmjs.com/ Example:

"dependencies": {
    "express": "4.17.1",
    "xyz": "git+https://github.com/bar/xyz.git"
}

If the package xyz is hosted correctly on www.npmjs.com and is added to package.json, the npm install will download correctly the xyz dependencies. If the package is special or is under development, you could have this problem.

This file package-lock.json is very important. If you delete it (as several answers advice) you could have another severe issues: Deleting `package-lock.json` to Resolve Conflicts quickly

Finally, if you have this kind of errors in which foo dependencies are not downloaded:

{
  "name": "acme",
  "version": "1.0.0",
  "dependencies": {
    "foo": "git+https://github.com/bar/foo.git"
  }
}
  • validate that in acme/node_modules don't exist the foo dependencies
  • Check the package.json of foo if the dependencies are coorectly added
  • delete the node_modules of your local project (acme in the example)
  • delete package-lock.json
  • run npm install
  • validate that in acme/node_modules exist the foo dependencies
  • push this renewed file package-lock.json to your git repository
JRichardsz
  • 14,356
  • 6
  • 59
  • 94
0

There might be a version conflict. I had the same issue, on npm 8.

At first, I tried to manually install the missing dependency using npm instasll <module> but npm complained and gave error that there is a version conflict between dependencies of two packages. I found out that two of the packages needed different versions of the dependency. I resolved the conflict, removed node_modules and after a npm install the missing dependency was installed as expected without manually installing it.

Samim
  • 91
  • 1
  • 3
  • 4