0

I've been working on a lot of different node.js projects. All of them have their own package.json file with their own needed packages. Every time I run node <mainfile>.js, npm installs all the packages to the project directory. Like so: C:/Users/me/Projects/<project-name>/node_modules.

This isn't a very big problem, but is there a way to make npm use/install to the global packages? Like in C:/Users/me/node_modules? One of the advantages I could see this having is less storage being taken up, although it isn't a huge advantage.

I would assume that if it is possible, it would require you to add/modify something in the package.json file.

While looking into answers for this question, I've seen people saying that you should avoid installing packages globally. Can you also explain why this is a bad practice andy why I should avoid it?

Dharman
  • 30,962
  • 25
  • 85
  • 135
Helix
  • 162
  • 3
  • 14

2 Answers2

1

Install Package Globally

NPM installs global packages into //local/lib/node_modules folder.

Apply -g in the install command to install package globally.

npm install -g express

To answer your other question

The obvious short answer is that your project depends on them. If your project depends on a package, it should be documented in package.json so that you can guarantee that it is installed when someone types npm install. Otherwise, you’ll need to add extra steps in your README file to inform anyone else who clones your project that they need to install each of your global dependencies as well

Finally, even if someone installs the correct version of Browserify for your project, they may be working on a different project that requires a different version of that same tool, which would cause conflicts. Several of your own projects might even use different versions of Browserify because you updated it when you started a new project and didn’t go back to make sure that earlier projects were updated to work with the new version. These conflicts can be avoided.

You can only have one version installed globally. This causes problems if you have different projects that rely on different versions of a package.

Bijin Abraham
  • 1,709
  • 2
  • 11
  • 25
1

Why not to install all packages globally

It's not really you shouldn't install a package globally it's more knowing what packages to install globally. The packages to install globally are ones that your project/application does not depend on.

How to identify a package that my project depends on

A package that your project is depended on is a package that your application could not run without like axios or express (an express API could not run without express installed or a web page that makes API requests with axios cant make those requests without axios) but something like http-server or minify is not needed to run the application so it can be installed globally.

Why is it important to have locally installed packages

It's important/good practice because if you are working with a group of developers or someone gets your work from Github they can just run npm install and get all the packages with out having to find all the packages them selfs.

How can I remove the node modules folder

You could technically globally install every package but I would sudjest not to. Node and many other developers know this is an issue that they have created a solution for in deno "the node killer".

I would recommend not installing all packages globally and if the node modules folder really annoys you try deno it fixes a lot of things that node developers hate.

Fletcher Rippon
  • 1,837
  • 16
  • 21
  • This still doesn't answer my question - is there a way to make npm use/install to the global packages? – Helix Aug 03 '20 at 12:43
  • Have a look at the link below (the accepted answer) this is probably what you want but he also subjects to not do this if you plan to deploy your application. https://stackoverflow.com/questions/7970793/how-do-i-import-global-modules-in-node-i-get-error-cannot-find-module-module – Fletcher Rippon Aug 04 '20 at 03:53