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 nonode_modules/
yet),npm list
andnpm 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 inC:\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?
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 :)
Not sure why it’s doing it, but the way to avoid it is to initialize your project directory using:
or if you don’t want to answer the questions:
That will setup the directory with the
package.json
andnode_modules
will be put there.Welp, turns out I've been mistakenly
npm install
-ing packages withoutpackage.json
. The first time I did this, I was in my home directory(C:\Users\{MyName}\
). This caused npm to createnode_modules/
andpackage-lock.json
in the home directory. Further (mistaken) attempts to install packages in my projects--which were still missingpackage.json
--caused npm to traverse upwards, until it found the initialnode_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