80

I'm completely new to javascript development and I'm getting the following error as I work my way through the backbone_blueprints book. Here is the error I get:

> simple-blog@0.1.0 start /Users/noahc/Projects/backbone_blueprints/blog
> nodemon server.js


module.js:340
    throw err;
          ^
Error: Cannot find module '../lib/cli'
    at Function.Module._resolveFilename (module.js:338:15)
    at Function.Module._load (module.js:280:25)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.<anonymous> (/Users/noahc/Projects/backbone_blueprints/blog/node_modules/.bin/nodemon:3:11)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)

npm ERR! Darwin 14.1.0
npm ERR! argv "node" "/usr/local/bin/npm" "start"
npm ERR! node v0.10.33
npm ERR! npm  v2.1.11
npm ERR! code ELIFECYCLE
npm ERR! simple-blog@0.1.0 start: `nodemon server.js`
npm ERR! Exit status 8
npm ERR! 
npm ERR! Failed at the simple-blog@0.1.0 start script 'nodemon server.js'.
npm ERR! This is most likely a problem with the simple-blog package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     nodemon server.js
npm ERR! You can get their info via:
npm ERR!     npm owner ls simple-blog
npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request:
npm ERR!     /Users/noahc/Projects/backbone_blueprints/blog/npm-debug.log
☹ ~/Projects/backbone_blueprints/blog npm install                                                                   ruby-2.1.3
npm WARN package.json simple-blog@0.1.0 No repository field.
npm WARN package.json simple-blog@0.1.0 No README data

Here is the debug log:

0 info it worked if it ends with ok 
1 verbose cli [ 'node', '/usr/local/bin/npm', 'install' ]
2 info using npm@2.1.11
3 info using node@v0.10.33
4 verbose node symlink /usr/local/bin/node
5 error install Couldn't read dependencies
6 verbose stack Error: ENOENT, open '/Users/noahc/Projects/backbone_blueprints/package.json'
7 verbose cwd /Users/noahc/Projects/backbone_blueprints
8 error Darwin 14.1.0
9 error argv "node" "/usr/local/bin/npm" "install"
10 error node v0.10.33
11 error npm  v2.1.11
12 error path /Users/noahc/Projects/backbone_blueprints/package.json
13 error code ENOPACKAGEJSON
14 error errno 34 
15 error package.json ENOENT, open '/Users/noahc/Projects/backbone_blueprints/package.json'
15 error package.json This is most likely not a problem with npm itself.
15 error package.json npm can't find a package.json file in your current directory.
16 verbose exit [ 34, true ]

And this:

☹ ~/Projects/backbone_blueprints/blog which node                                                                    ruby-2.1.3
/usr/local/bin/node
☺ ~/Projects/backbone_blueprints/blog which npm                                                                     ruby-2.1.3
/usr/local/bin/npm

I thought it might be a path issue and so I added export NODE_PATH=/opt/lib/node_modules to my .zshrc file and sourced it and that seemed to have no impact.

Any thoughts on anyway I can troubleshoot this or try to better understand what is actually happening?

Noah Clark
  • 8,101
  • 14
  • 74
  • 116
  • I got this error after starting an accidental `npm i --save-dev [package]` and then immediately cancelling. The answer with the highest votes solved the issue for me. – aggregate1166877 Jun 02 '23 at 13:56

15 Answers15

128

My solution was to just delete the directory /node_modules/ and to make a fresh install afterwards with npm install.

Googol
  • 2,815
  • 2
  • 22
  • 13
  • 1
    Can confirm that this worked after I changed the root directory name (and `npm run – lol Oct 05 '16 at 03:02
  • 23
    `rm -rf node_modules/ && npm i` was also the solution for me. :) – Milkncookiez Dec 20 '16 at 10:19
  • 24
    For those who would like to know (at least one reason) why - this can happen when you copy a `node_modules` directory (using `cp` for example). By default the `.bin` directory contains symlinks to the installed packages. `cp` does not preserve links by default, and thus you'll end up with actual files in the `.bin` directory. This is what breaks the require() function. Blowing away `node_modules` and running `npm install` restores the symlinks that should have been there. – Colin M Apr 03 '18 at 21:58
  • 4
    Don't forget to also wipe package-lock.json. `rm package-lock.json`. All together: `rm -rf node_modules/ && rm package-lock.json && npm i` – rsmets May 30 '20 at 00:06
40

Nodedemon is looking for its ../lib/cli folder in its own directory and not finding it. You probably have a messed up install. This can happen sometimes when you use finder to copy a project from one folder to another.

If you blow away your node_modules directory and reinstall from scratch, you should restore the folder.

cd your_project_folder
rm -rf node_modules
npm install

Edit:

You might also use yarn.

cd your_project_folder
rm -rf node_modules
yarn
superluminary
  • 47,086
  • 25
  • 151
  • 148
39

For me npm was broken, probably because I ran it with sudo at some point.

I needed to fix brew, upgrade/update

brew uninstall node
sudo chown -R $(whoami) /usr/local/*
brew cleanup
brew install node
Lamellama
  • 621
  • 6
  • 4
15

I found the fix. I had to install nodemon globally doing this: npm install nodemon -g

Noah Clark
  • 8,101
  • 14
  • 74
  • 116
  • 3
    I read that as 'no demon' for a good five minutes .. if only someone would write THAT package. – Brandon Prudent Apr 12 '16 at 22:47
  • I'm not a fan of global npm installation. If `nodemon` is a dependency of the project, I'd rather add `./node_modules/.bin` to my `$PATH` – fpietka Jun 14 '16 at 14:53
  • `npm` does that inside of the `"scripts"` section of a `package.json`. So you can have something like `"start-dev": "nodemon --exec DEBUG=app* node_modules/.bin/babel-node -- app/start.js"`. – juanpaco Jul 12 '16 at 21:25
8

I had this error, leaving this here in case it helps anyone.

I installed yarn and apparently messed up my node install, tried to fix it by uninstalling stuff, made a total mess. Brew wouldn't install node successfully (old OS maybe out of LTS, installing from source code wouldn't make npm work).

Even

npm -v

would give the error (so nothing to do with node_modules). Finally fixed it by installing node using a package manager from here.

Pam
  • 1,146
  • 1
  • 14
  • 18
6

In macOS BigSur i had to clean up the node modules folder first:

sudo rm -rf /usr/local/lib/node_modules

Note: Reason i needed sudo is some of the folders need special permission to remove them.

After removing node_modules I had reinstalled npm package by:

brew postinstall node

Finally i was able to see npm back into my mac:

npm -v
7.21.1
ArunSelvam P M
  • 429
  • 5
  • 6
5

You can try doing the following

npm uninstall nodemon
npm install nodemon
Chad Cache
  • 9,668
  • 3
  • 56
  • 48
4

So npm creates a symbolic link between modules that have an executable declared in the package.json file and the node_modules/.bin directory.

Read from npm to understand.

Dr. Axel Rauschmayer explains more here.

When ever a project directory and its node_modules folder is copied using macos finder, windows file explorer or command line, this symlink is destroyed. Thus calls to the symbolically linked file on the node_modules/.bin directory cannot hit the supposed package.

In your case it was nodemon . To manually fixed this run

ln -s ./node_modules/nodemon/bin/nodemon.js ./node_modules/.bin/nodemon

for any other package, check the "bin": { } key in it's package.json and see the the symlinked executable, then modify the link command above and run according.

Miracool
  • 657
  • 6
  • 7
  • For my case in which I also had copied project folders to learn things, removing `package-lock` and `node_modules` and reinstalling the packages on the copied folder which had the issue did the work. – George Linardis Aug 26 '22 at 17:57
1
brew uninstall node
brew upgrade 
brew install node
brew link --overwrite node 
npm -v

Worked for me.

shirish kumar
  • 47
  • 1
  • 4
1
brew link --overwrite node

Is all that is needed. No need to uninstall.

joland
  • 1,191
  • 2
  • 8
  • 7
0

Had the same problem.

I use protractor --elementExplorer and it works for me

guy mograbi
  • 27,391
  • 16
  • 83
  • 122
0

Protractor have changed the location.The lib/built protractor files now reside in the built directory of the package. You should just be able to direct your IDE to built/cli.js This worked for me.

Check their Github Issue link :

https://github.com/angular/protractor/issues/3089

user3444999
  • 481
  • 4
  • 10
0

I was getting same error after installing nodejs using homebrew to fix it I have downloaded and installed nodejs from it's official website. It's working fine now.

Uddesh Jain
  • 1,064
  • 2
  • 15
  • 16
0

Got this error whilst building docker image with line RUN apt install -g serve Fixed it by changing the method to

RUN  curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
ENV NVM_DIR=/root/.nvm
RUN . "$NVM_DIR/nvm.sh" && nvm install ${NODE_VERSION}
RUN . "$NVM_DIR/nvm.sh" &&  nvm use v${NODE_VERSION}
RUN . "$NVM_DIR/nvm.sh" && nvm alias default v${NODE_VERSION}
RUN cp /root/.nvm/versions/node/v${NODE_VERSION}/bin/node /usr/bin/
RUN cp /root/.nvm/versions/node/v${NODE_VERSION}/bin/npm /usr/bin/
RUN /root/.nvm/versions/node/v${NODE_VERSION}/bin/npm install  serve -g
0

I tried most of these solutions, minus yarn and brew, to no avail. What ended up fixing it was:

curl -qL https://www.npmjs.com/install.sh | sudo sh

And voila, it was all better!

J. Cravens
  • 131
  • 1
  • 7