How do I restore npm root folder to the default one? - node.js

I just installed ionic with npm and when I run -v to check the version it gives me an error and that says "ionic is not recognized as an internal or external command". Basically it's like I never installed the package in the first place. After a while I figured that my default folder for npm has changed.
I'm using Windows 10 so I think it should be something like
User\MyUser\AppData\npm\node_modules" or maybe "User\MyUser\AppData\Roaming\npm\node_modules" but for whatever reason npm installs stuff inside "AppData\Local
I asked around a bit (even here on stackoverflow) and found out that if I edit the environmental variable "PATH" things should work, so I did it and ionic now works fine
However today I tried to install express and the same error popped up. I checked my PATH again just to be sure and it's pointing to the "Local" folder where my node_modules are. Ionic still works right now and it's in the same folder, so I have no idea why express doesn't work.
I want to fix things once and for all, how do I change the place where the node_modules are? How do I go back to AppData or Roaming or whatever folder Windows 10 uses by default?

Related

Unable to delete node_modules folder

I am trying to delete the node modules folder from my project so that I can rerun npm i as there were errors when I was running my npm scripts (I think node_modules corrupted), but I don't seem to be able to.
I've tried:
Deleting via right clicking in Windows explorer. This just doesn't do anything.
In VS Code right clicking and selecting 'Delete Node Modules', which produces the following message:
I think the node modules file has corrupted - there only seems to be 3 folders in there, all of which I can't delete.
If I run npm i I get the following:
Try to reboot windows with SAFE MOOD.
I face the same issue on windows 10. I could delete it when I have been in a safe mood on windows.
Don't know really an issue on windows or my hard drive.

Problems Reinstalling Node js for webpack

I am installing webpack manually. The problem seems to be that the environment variable is set incorrectly. So to reinstall nodjs, I learned and ran the code 'rm -fr /usr /usr/local/bin/{node,npm} /usr/local/lib/node_modules/'.
However, the message 'Remove-Item : Could not find a parameter that matches the parameter name 'rf''.
The path of the code should be used to fit my computer.I changed the code because I wanted to, but the same message appears.
How can I solve this?
Looking at your folder structure, I think you are using a linux based operating system. To reinstall nodejs, you can refer to this article by digital ocean, which explains briefly on installing and uninstalling nodejs on linux.
Also, if you would like remove a folder in linux or using git bash, navigate to the parent folder of node_modules, run
rm -rf node_module/
Similarly you can do it for the bin directory for {node,npm}. Also if you are looking at setting up webpack for react, you could follow this medium article by Marcos Lombog on A Complete Webpack Setup for React.

Many unnecessary files generated by 'npm install' in root directory of app

Usually when I do 'npm install' inside my application directory, bunch of npm libraries files gets generated inside node_modules folder which is expected.
Today suddenly I started seeing many files getting generated inside application directory and outside node_modules.
Did anyone face this issue ? and if yes - any workaround ? Screenshot attached. Lot's of .cmd file, don't know why they are here.
Edit your c:\Users{username}.npmrc and remove the prefix.
I had the same issue, and was able to resolve this by completely uninstalling + re-installing Node/npm:
How to completely remove node.js from Windows

Finding node module from different directory?

Always feel stupid asking here because people are always confused with my questions, or I have a dumb problem, but, I'm working on a program in node.js and the text editor I'm using (NP++) doesn't seem to like to save files in the system32 directoy, (The directory where my modules are), and that is where my script is as well. (So I have .../.../node_modules/(modules) and .../.../node_modules/script.js) this becomes a pain when I want to edit the script, I have to clone the script to my desktop, then edit it, then overwrite the one in the node_modules directory. I tried saving the script to my desktop and running it, but it just gives me an error of module not found. (In my script I have the modules as var example = require('example.js')) Is there any way I can get it to get the modules from the node_modules directory, while keeping the script file somewhere easily accessible and editable? (i.e desktop?) (Sorry if this is confusing, not the best at these kind of things)
I'm not 100% sure that this is what's happening because I haven't used npm on Windows, but it sounds to me like you're installing your dependencies globally using npm -g. The more proper way to use Node is to install your dependencies locally, using npm without the -g flag. That way your dependencies get installed in your current working directory.
For example, let's say you've saved your project in a directory on your Desktop, and your script uses require("lodash"). If you cd to your directory and run npm install lodash, then the lodash module will be available to your script.

Understanding npm and Node.js install location for modules

I've been using Node.js and npm for a few weeks with great success and have started to question the best practice for installing local modules. I understand the Global vs Local argument, however, my question has more to do with where to place a local install. Let's say that I have a project located at ~/ProjectA/ which is version controlled and worked on by multiple developers. When initially playing with Node.js and npm I wasn't aware of the default local installation paths and just simply installed the necessary modules in a default terminal which resulted in a installation path of ~/node_modules. What this ended up doing is requiring all the other developers working on the project to install the modules on their own machines in order to run the application. Having seen where some of the developers ran npm install I'm still really surprised that it worked on their machines at all (I guess it relates to how Node.js and require() looks for modules), but needless to say, it worked.
Now that the project is getting past the "toying around" stage, I would like to setup the project folder correctly. So, my question is, should the modules be installed at ~/ProjectA/node_modules and therefore be part of the version controlled project files, or should it continue to be located at a developer-machine specific location...or does it not really matter at all?
I'm just looking for a little "best-practice" guidance on this one and what others do when setting up your projects.
I think that the "best practice" here is to keep the dependencies within the project folder.
Almostly all Node projects I've seen so far (I'm a Node developer has about 8 months now) do that.
You don't need to version control the dependencies. That's how I manage my Node projects:
Keep the versions locked in the package.json file, so everyone gets the same working version, or use the npm shrinkwrap command in your project root.
Add the node_modules folder to your VCS ignore file (I use git, so mine is .gitignore)
Be happy, you're done!

Resources