162

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
  • 15,913
  • 8
  • 54
  • 94
Ahmed Nuaman
  • 11,324
  • 13
  • 50
  • 80
  • 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

17 Answers17

160

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,163
  • 1
  • 10
  • 22
allprog
  • 15,913
  • 8
  • 54
  • 94
  • 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
  • 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
126

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
  • 58,878
  • 30
  • 149
  • 175
  • 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
45

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
  • 5,646
  • 14
  • 33
  • 35
Virat18
  • 2,797
  • 1
  • 21
  • 28
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. – Monkpit 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
10

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
  • 107,550
  • 65
  • 332
  • 414
Nrj
  • 6,226
  • 6
  • 44
  • 56
10

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

After that just reinstall everything with npm install.

Lars Kliesing
  • 101
  • 1
  • 5
10

Also check that your package name is correctly accepted:

WRONG:

{
    "name":"My Awesome Package"
}


CORRECT

{
    "name": "my-awesome-package-name"
}
Caius
  • 1,547
  • 4
  • 20
  • 42
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
  • 9,741
  • 6
  • 40
  • 56
  • 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
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
  • 2,759
  • 24
  • 23
  • 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,755
  • 1
  • 27
  • 39
3

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
  • 134
  • 1
  • 4
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
  • 3,837
  • 2
  • 35
  • 53
3

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
  • 315
  • 1
  • 2
  • 11
  • 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
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
  • 615
  • 7
  • 12
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
  • 647
  • 5
  • 20
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
  • 718
  • 5
  • 20
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
  • 361
  • 2
  • 14