5

Node.js newbie here, Windows 10. I npm install-ed some packages (without -g) while inside a directory that didn't have package.json. npm placed the packages in C:\Users\{MyName}\node_modules\.

Now I'm seeing some weird behavior:

  • When I'm in my project directory (has package.json but no node_modules/ yet), npm list and npm list -g both show an empty list
  • When I'm in a non-project directory (no package.json)...
    • npm list -g still shows an empty list
    • However, npm list shows everything in C:\Users\{MyName}\node_modules\

Question 1. What is going on here? Apparently, npm's default global path should be C:\Users\{MyName}\AppData\Roaming\npm\. If so, why is it using C:\Users\{MyName}\node_modules\?

Question 2. How do I get out of this mess? Node.js has no problem importing packages from C:\Users\{MyName}\node_modules\, but I want npm to list them properly. How can I delete the semi-global packages, reinstall them correctly, and ensure that this doesn't happen again?

Phil Kang
  • 707
  • 6
  • 16

4 Answers4

15

Welp, turns out I've been mistakenly npm install-ing packages without package.json. The first time I did this, I was in my home directory(C:\Users\{MyName}\). This caused npm to create node_modules/ and package-lock.json in the home directory. Further (mistaken) attempts to install packages in my projects--which were still missing package.json--caused npm to traverse upwards, until it found the initial node_modules/ dir, and install everything there. Because my home directory is among the places Node.js looks for modules, I didn't notice my mistake until now. :P

Phil Kang
  • 707
  • 6
  • 16
2

Not sure why it’s doing it, but the way to avoid it is to initialize your project directory using:

npm init

or if you don’t want to answer the questions:

npm init -y

That will setup the directory with the package.json and node_modules will be put there.

sɐunıɔןɐqɐp
  • 2,877
  • 15
  • 33
  • 38
user2771365
  • 112
  • 1
  • 7
  • Welp, I never know `npm init` existed, I thought everyone was writing `package.json` by hand. Thank you! – Phil Kang Jun 17 '18 at 08:34
  • @user2771365, it would be more polite to direct a new user to instructions on [what to do when someone answers](https://stackoverflow.com/help/someone-answers) their question then telling them to accept your answer to increase your reputation. – Squashman Jun 21 '18 at 04:24
0

Ok, a couple of tips then...

when you install a package that you are going to use in production then add --save, e.g.

npm install --save some-package

this will automatically add the dependency to your package.json. If you are installing a package for use purely in development, e.g. chai, then use

--save-dev

and it will add it to the development dependencies.

Also, git is your friend, even if you are only messing :)

Happy noding :)

user2771365
  • 112
  • 1
  • 7
0

For me the solution here was:

  1. Go to c:\users[me]\AppData\Roaming\npm and delete the node_modules folder completely
  2. Make sure I had the package.json file for the project
  3. Delete the project package-lock.json file
  4. Run npm init
  5. Run npm install

Project then worked, not sure why the node_modules got to be in the folder above, ain't got time to find out.

GrahamJ
  • 508
  • 4
  • 14