简体   繁体   中英

npm keeps installing globally

I'm using latest npm, on a mac. I've got a strange problem: if I want to install some package locally on a folder, I'm forced to npm init it. In other words, if I simply npm install [package name] , it will install globally (in my /User/npm-modules folder), and thus the onnly way to install locally is to init the folder. So basically, the opposite it should normally do. Why? Any idea?

npm init creates package.json file in your current project directory. npm packages will install locally when you have package.json set up by using the command npm i PACKAGE-NAME .

Yes, if you want to install package localy, you need to create package.json file. This is what npm init will do and then you can install packages

I haven't worked with node in a while but back in my days, npm used to look for node_modues folder starting from current directory and keeps moving to the parent folder. If no node_modules folder is found then it will go to the global folder.

So in your case create an empty node_modules folder in your current directory and try npm install [package]

This should avoid the FORCED npm init you mentioned. However, a cleaner solution is always to use npm init .

The npm idea was to separate different apps and their dependencies in a single project. Looking for node-modules in the same folder helped to achieve this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM