Solving Path Problem - Running NPM modules/packages in Windows 10: NPM searching for module in wrong directory.
Computer looking to execute npm packages from wrong directory; Solved by adding\editing order of Windows environment variables in Control Panel.
I spent a few hours trying to figure this out, but what it finally came down to was adding the path where the packages were being installed to the Windows PATH environment variable, and moving it to the top of the list.
NPM seemed to be searching in the wrong directory for the package to execute. The error message is shown below:
I had found out through some Googling that the npm packages were being installed in the “[User]\AppData\Roaming\npm” directory.
Doing some more Googling I read that the ‘environment variable path’ for ‘User Variables’ had to be changed. This was easily done through the Control Panel. I believe that the correct paths were not yet there when I began, and I had to paste them in. See screenshots below Control Panel flow to get to environment variables:
Below we see that the program files\nodejs path and the roaming\npm paths are both in the environment variables. If they are not there for you, just click “New” and paste these paths in. Then move the roaming\npm path to be above the program files\nodejs path.
After this has been done, clicking ‘OK’ on the last two Control Panel windows and then restarting the command line should do the trick of allowing the npm command to be called from the correct directory:
And there you have it! Good luck to you, and may you live free.
😃
— — — — — — — — — — — — — — — — — — — — — — —
Another issue I ran into: me using the wrong npm commands without knowing it.
I had another problem before the PATH problem, which was that my commands weren’t working. I figured out what my first problem was after some testing. It was just that I was trying to run the packages using invalid commands. In the next part of this article I show how I investigated the incorrect path problem to find my error.
This section between the stars is just me figuring out that I was typing in commands wrong. Probably unnecessary reading unless one is interested in the debugging process.
At first when I was trying to run an npm package from the command line my problem was that the command seemed to not even be recognized. I got the following error message:
“ ‘nodemon’ is not recognized as an internal or external command,
operable program or batch file.”
A screenshot of this type of error is shown below, but the screenshot is from an error I initiated by typing the name of the package incorrectly, while originally it was giving me this error message even when the name was typed correctly.
I’m going to try and install another npm package globally to see if we get that first problem of the command not being recognized.
I now tried to install another npm package, cat-me, and see if it was placed in the roaming\npm folder.
However, I did install cat-me and faker locally in the roaming\npm folder. Now let me install a new package globally, and see if I can run it, without installing it locally in the roaming\npm folder:
So the installations are working apparantly.
End of my debugging process that led me to find out I was using the wrong command
*********************************************