Can you prevent node.js from installing packages locally? (Use global packages) - node.js

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?

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.

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.

Related

Is there a way to tell NPM NOT to use globally installed packages during a build

We work in a large team on multiple projects and everyone has a different set of node packages installed globally.
This can cause different behaviour when different members of the team build a project.
Is there a setting in a package.json file (or other file) that we can apply that forces NPM to only use local packages that have been installed in the local project node_modules.
I assume that this is more relating to development tools and modules (netlify, standard, prettier, #babel/core, rollup, eslint, etc)
Using npm i --save-dev package-a package-b package-c to ensure that you have the exact and correct versions across your development teams. I would suggest looking at package.jsons in projects like three.js, react, nextjs etc to see how they handle it. It can be quite a task to get a correct baseline of devDependencies, but once set, it generally solves a lot of package dependency problems caused by global modules.
See this: http://npm.github.io/how-npm-works-docs/npm3/how-npm3-works.html

How to set global node modules so that all application can use same

I am new to node, i have made few small application using node, but everytime i have to use npm install for every application which download the required dependencies in node_modules folder. There are many libraries which are common.
I tried installing using npm install express -g but i was not sure how to use this dependency in other application which is in some other folder.
Is there any way i can have only one folder like in D:\Users\User\AppData\Roaming\npm\node_modules from where my all applications can have the module which they need ?
Can anyone let me know how to do the settings for the same ?
Any help would be highly appreciated !!
Every node application that has a package.json has a specific set of rules for using specific versions of it's modules. You can install globally only one version of a specific module, but if you happen to have an application that needs an older / newer version that is not installed globally on your dev environment, then it will fail to work.
The recommended way of using node modules ( packages ) is to have a local directory inside your project, which contains all libraries that the project needs. This practice is everywhere and so you should follow it.
There are some ways to mitigate the slow npm install, though.
There is a new npm-replacement, created and maintained by Facebook, called yarn.
What yarn does is it creates a local cache of all installed packages and then symlinks them to your project folder from your local computer cache. This way the npm install procedure becomes very fast.

Doesn't npm install check for a global version first?

I just setup a test, and tried to npm install express even though express already exists globally on my system. To my surprise, instead of using the global version, it ended up re-installing a version locally!? Isn't it supposed to use the global version... Or am I suppose to use -g every time, even when I only want to use the existing global version. Otherwise, what's the point of installing anything locally!?
The answer is "NO". It isn't supposed to use your global version.
If you want to use your global version, then you doesn't need to execute npm install at all because it is already installed.
If you do it then, obviously, you are saying "I want to install it locally to my project". And more than that: "I want to install its latest version unless it is declared in my package.json with other explicitly specified version".
In fact, the actual question is: Why in the hell would you want to not install a dependency of your project locally? To have more version mismatch issues?
As #anshuman_singh says, best practice is to always do an npm install --save.
You are able to use globally installed packages, of course. It could be handy for fast testing code that you will drop just after a few hours or so.
But, anyway: If you doesn't have really hard disk or network bandwidth issues, installing all dependencies locally will avoid you too much trouble in the future.
On the other hand, uploading that modules to your code repository is also a bad idea (maybe that is what you were trying to avoid) because, with different versions of node, most native modules won't work if not rebuild. But most VCS support ignoring files and or directories that must not be uploaded.
For example, in git (.gitignore file):
**/node_modules
In summary:
npm init (if you didn't already it).
npm install --save for all your project dependencies.
npm install --save-dev for dependencies not needed in production (testing stuff).
Don't upload node_modules to your VCS.
After new checkout: npm install or npm install --production (to not install dev-dependencies).
npm install -g only for tools you will use in console.
This way, you are sure that you will have in production (or other dev environments) the exact same version of each package.
And, finally, if you ever want to upgrade some package to its latest version, simply run:
npm install --save <pagkage_name>#latest.
If you’re installing something that you want to use in your program, using require('whatever'), then install it locally, at the root of your project.
If you’re installing something that you want to use in your shell, on the command line or something, install it globally, so that its binaries end up in your PATH environment variable.
The first option is the best in my opinion. Simple, clear, explicit. The second is really handy if you are going to re-use the same library in a bunch of different projects
Install locally-
npm install moduleName
install locally and save in package.json-
npm install moduleName --save
install globally-
npm install moduleName -g

cannot find module express, how to install it to make it globally available?

I want to experiment with some node.js stuff and I installed it yesterday following someone's instructions on the web which got it up and running, and I got the standard Hello World web page up on the screen.
I now went to move onto another example, but in order to not clutter my home directory, I created a directory off of it (~/node) and created the files I needed in there. Low and behold, when it came time to run the service, I got no joy stating the express module couldn't be found.
The instructions told me to install express using the -g flag, but that didn't help. I even ran it again without any luck.
Now I've found this:
Cannot find module `express` | socket.io [node.js]
and it appears I have to install it again under the current directory. I have done that and it works. Is it the case it has to be installed under each directory that I want services running from? It seems an unnecessary duplication.
edit:
Not knowing much about js I thought I would go digging and found
app.use(express['static'](__dirname ));
and have realised this is probably the cause of my problem. Further research has found this: http://nodejs.org/api/modules.html#modules_loading_from_node_modules_folders and if I install it once in a higher level directory, that should 'solve' my problem. I'm not going to bother about uninstalling the module, but for my next project I will try it and see how it goes.
I don't know why the original author suggested the -g flag when installing express, since it hasn't seemed to work for me.
NPM is a really nice tool, allowing you to install node.js modules locally and globally.
Local module installation
If you want to use a module for your project, you have to install it locally.
That's why npm creates a subdirectory called node_modules inside your project directory.
If you use the same module for two different projects, npm will download the module and install it twice. That's perfectly normal, it helps you manage different versions of the same dependency.
The best way to manage dependencies and install modules for a specific project is to fill the package.json with your dependencies and install them using
npm install
inside your project directory.
To access your modules in your code, use the require() function.
For example, with expressjs :
var express = require('express');
var app = express();
...
Global module installation
npm allows you to install modules globally as well. But remember that installing a module globally only provides more commands in your terminal, as with expressjs and express(1).
In order to install expressjs globally, run this in your terminal
npm install -g express
If you want to use a globally installed module in a specific project, you also have to install it locally (in your project directory, without -g).
I hope this answers clearly your question.
Express is capable of generating a simple app structure when installed globally. See this link and scroll to Using express(1) to generate an app section. It's a good way to get you started easily.
Take a look into package.json, package.json in nodejitsu
All npm packages contain a file, usually in the project root, called package.json - this file holds various metadata relevant to the project. This file is used to give information to npm that allows it to identify the project as well as handle the project's dependencies
a package.json example:
{
"name": "CRUD",
"description": "A simple CRUD",
"author": "Only for learn",
"dependencies": {
"express": "*",
},
}
so for install the dependencies go to level that package.json is, and run npm install this one will install all the dependencies you need for the project.
EDIT
a package.json interactive guide
I have found that when setting up node.js projects and dependencies, using Grunt [http://gruntjs.com/] has a lot of advantages. Although there are lots of different ways to setup a node and express project there is a lot to be said for using the Douglas Crockford approach and 'going with the grain'. In this case Grunt is the grain as it is becoming the de-facto standard for setting up a node project and there are existing templates for the most common types of node.js projects. You can find Grunt Express here [https://github.com/blai/grunt-express].
In this case Grunt would provide you with a project structure consistent with others, setup dependencies file for the node package manager and auto generate the express project for you. Packages are kept in a node_modules directory. If you are familiar with maven you might recognize the 'convention over configuration approach'.

Where does node.js put its files?

I have recently started playing with node.js, but I got lost in a big mess of different versions of node, npm, nvm and other packages. I don't know what is installed globally and what is installed locally (and if locally, how do the packages know which versions of node they can use?).
I'd like to have some summary of what different installation options do. In specific:
Where is node installed when I use nvm, apt-get, make install or when using other ways?
Is it a good idea to install node locally?
Why does nvm change my ~/.profile instead of installing itself in some system-recognizable bin folder?
I saw that nvm can install different versions of node alongside each other - why would I want to do this? I can install them locally instead, right?
Where does npm install packages? I saw that it checks packages aganist version of Node, what happens to these packages when node is upgraded?
In what cases it is better to use global or local installation? Where should I put my packages then (and where they put by default?)
What's the difference between npm, nvm and nave?
EDIT: There is a lot of ways to install node here, this makes me even more confused...
Where is node installed when I use nvm, apt-get, make install or when
using other ways?
apt-get installs all the software, not only node, on the file system following the Ubuntu convention where to store binaries, man files, shared files, logs, etc. However, using apt-get you'll have only the certain version of node which is determined by the distribution release cycle. If there are updates available they will be installed with apt-get update; apt-get upgrade However, the newest version of some app won't be available until it makes its way into the distribution. For example node v0.x.y might not be available until Ubuntu 13.10 the only way to get will be to install it manually. The good side of apt-get or other system package manager is that it manages updates and package removal for you. It stores all the data about the software package in it's own database. You can always delete the node with apt-get remove node and that's it.
make install install the package manually, but it is considered harmful. Never use the make install mainly because you won't be able to delete the package easily, you'll have to read the Makefile and manually delete all the files installed by it. In a situation where you want to use make install there is always checkinstall available. It's a software which creates a native package and registers it with the system. When you decide to delete the package you could do this with one command instead of many. wiki link; Ubuntu guide on checkinstall
Now nvm script is a node version manager. It is very helpful and easy to use. It allows you to have multiple versions of node to be installed and used in parallel on your machine. It doesn't compile the node from source like make install so it is very fast. it doesn't depend on your distribution release cycle so you have access to all the node versions available at the moment. nvm downloads precompiled binaries and is perfect for general use. It stores it's node files in it's own folder locally so in case you want to compare something between the different node versions it's easy to do.
Is it a good idea to install node locally?
If by locally you mean using nvm then it's very good for development, and testing. Not sure about production performance implications and benefits between having it's installed from source or using the nvm precompiled binaries. I use nvm for development and installed from source in production. However if someone could explain this issue any further I'll be glad to learn more.
Why does nvm change my ~/.profile instead of installing itself in some system-recognizable bin folder?
Because nvm isn't an executable. It is a set of bash functions which are sourced by shell and could be used separately. You can invoke nvm_ls and nvm_ls_remote and others without the main script after is is sourced into your shell. What the main script does it parses the command line arguments and pretty prints the output in case of for example `nvm_ls_remote'.
in the ~/.profile the following line is added
[[ -s /home/USERNAME/.nvm/nvm.sh ]] && . /home/USERANME/.nvm/nvm.sh # This loads NVM
loads all the functions into your shell
I saw that nvm can install different versions of node alongside each other - why would I want to do this? I can install them locally instead, right?
You can install them locally using make install or checkinstall but you will have to make aliases for them like node_0.8.1, node_0.8.2, node_0.10.1 , etc. AND you'll have to manage new aliases, installing all the packages, removing them in case you don't need them YOURSELF. These are a tedious and boring tasks which could be error prone sometimes. nvm does all of these tasks for you for free.
You want to do this to test your app under the different versions of node. For example you are good and tested under the v0.8 but you want to use the new features of the v0.10.3 how do you do that ? You have to download the source code, compile, make an alias and run your app. you could do this with just nvm install 0.10.3 and run your app.
Sometimes you have to support more than one version of node. For example some hosted environments are not keeping in touch with the latest release and only have v0.6 Your clients which use your server app might encounter a bug specific to this version. When you fix the bug you have to reproduce it first. Using nvm installation of the v0.6 is one line and half a minute. And you can check all the versions you want this way easily. Test your code under different versions and make sure you are good to go.
Where does npm install packages? I saw that it checks packages aganist version of Node, what happens to these packages when node is upgraded?
If you are using nvm the packages which are installed globally with -g option are tied to the relevant node version. When you switch between versions with nvm use 0.x you have to either install the packages again or use nvm copy-packages <version> to use the packages from in the current version. If the packages are installed locally then it depends. package.json should contain info on the dependencies of the app. If it says node: '0.8' and you just updated to 0.9 you might encounter troubles. For example the behavior of process.nextTick was changed in the latest releases compared to 0.6. So be careful.
In what cases it is better to use global or local installation? Where should I put my packages then (and where they put by default?)
It depends. For development nvm is superior in my opinion. For me it is convenient and simple. For production there are might be some performance implications when using the precompiled binary files not optimized for your system. It would be better to ask this as a separate question so the people with the relevant experience could answer.
What's the difference between npm, nvm and nave?
npm is a node package manager -> link It contains userland packages developed by other people. These packages are not part of the node core. npm is used for publishing your code and dependency management. If your app requires other app developed by other people it is convenient to publish it via npm.
nvm is a node version manager it does a completely separate thing. It gives you an ability to very easily switch between node versions on the same machine and manages all he changes in your $PATH environment variable.
Consider nvm as update manager for the Operation System and npm as a manager of the applications for this system. Well, this comparison isn't precise but just came upon my mind
nave is basically the same as nvm but it is an executable whereas nvm is a script which is sourced into the shell. Each system has it's own benefits. You could make a separate question regarding it's use cases and differences.
My answer isn't 100% complete and contains a lot of subjective personal opinions. However, I hope I'll at least make some points more clear so you might proceed with other more specific questions. Btw, this question list of yours could be asked as separate questions. I believe stackoverflow gives best results when specific questions are asked separately and more people with relevant experience could contribute.
If you run
npm install
in folder with package.json, it installs all packages localy (in the current folder).
Also, npm default install packeges local. To install it globaly - use -g flag:
npm install -g <package>
Execute next command:
npm config list
You see all npm config description.
You can install modules in the local context of your application with
npm install modulename
In this case the module will be installed to your node_modules folder of your application.
Otherwise you can install a module in the global context with
npm install -g modulename
In this case the module will be installed for the hole system environment usually at /usr/local/bin/modulename.
The global installation makes sense for modules you need in more than one application, like express or node-inspector.

Resources