Can/Should gulp livereload be installed globablly - node.js

I am new to Node and I don't fully understand yet what installing locally means exactly. I know I need to install gulp both globally as well as locally but in my case I have a number of projects in separate folders under a development folder and I wonder if I really need to install the livereload extension locally in each separate project folder (which is what the documentation seems to suggest) would it not be easier to install it globally; or locally in the main development folder. Can someone explain how this works and what options I'd have.
Similarly I wonder whether if I install gulp locally in the development folder will this be available to each of it's children or whether I'd again need/want to install it in each project folder locally.

Here is an, albiet old, article on the node js blog that goes over locally vs globally. http://blog.nodejs.org/2011/03/23/npm-1-0-global-vs-local-installation
The basics are that if you want to require require('gulp-livereload') inside of your code it should be a local (dev) dependency. If you wish to interact with the module via the cli then install globally. For example you might have both gulp and nodemon installed globally.
The point of local install is that someone can bring down your project, type 'npm install' and get all of the dependencies local to that app.
So to answer your question install gulp-livereload locally. But other modules such as gulp, nodemon, etc you would have both global and local.
As an extra help if you want to install gulp as a project dependency but have it globally as well you can run 'npm link gulp' in order to keep them in sync.

Also, you can have just one global version while, using the local one, you can use for a specific project the specific version used while developing it.

Related

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.

How to do a manual install of Node js package dependencies

I am rather new to Node but am working on a project behind a very restrictive firewall so I cannot use npm to install packages (no proxy either). I am trying to use express and have been able to include it by just storing the files locally and requiring the local file path but I do not know how to structure the project so that node can find and include all of the dependencies for express (which I also have manually downloaded from github and stored locally within the project). Does anyone know how to do a completely manual global or local install of individual node dependency packages?
Simply download the files and place them in a 'node_modules' folder in the root of your app. Then use 'npm init' to create a package.json. Add your dependencies in the dependencies list in that file.
Then run simply 'npm install'.

Skip local installation of Gulp

Can I somehow skip local installation of Gulp to the project?
I installed Gulp globally, added it to package.json as a dependency. But still Gulp wants to be installed locally also with the Local gulp not found in ... message. If I do local install, Gulp is copied into my project by node.
Is there a way to skip local installation of Gulp? I want to be able to run it across the whole server from command line and manage it installation globally.
One clean way to do this is without have it locally is to install gulp globally like as you did and then run in your directory the command :
npm link gulp
It will create a symbolic link in your node_modules folder to your global gulp install. You need to be careful on the versions since all your symlinks and therefore your local project(s) will use the global one, no matter of the defined version in the package.json, which could cause errors on a deployed environment.
This is also applicable for every node package, and allow you to update the local install you've created that way of multiple projects at once.

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'.

Using compiled node dependencies on different environments

I have a node project that I run locally and deploy to a CentOS server. I have a few dependencies, one of which relies on a compiled dependency.
Using npm, I can compile the dependency locally using npm install, and it'll work great. This however is not compiled for the deployment server, so it will break if it is uploaded.
I can compile the dependency on a CentOS box, and it'll work on the server, but it'll break in the local environment.
Can anyone think of a workaround so that I can force node to use the global dependencies in the local environment, and a the local ones on the server?
Thanks!
Update:
I've figured out a way to do this for now, it's not elegant, but it works:
run npm install on my local environment
rename the node_modules folder to node_modules_local
run npm install on the server
prepend a variable to all require() paths
set a local environment variable to "development"
check if we're in development mode, if we are, set the require path variable to ./node_modules_local.
Check in the source code of the dependencies (compiled dependencies and js-only-dependencies). When you deploy your app run 'npm rebuild' on the server. Seems like this is the officially recommended way to go. See http://www.mikealrogers.com/posts/nodemodules-in-git.html for details

Resources