/*
INSTALL LOCAL PACKAGE
>npm help install
Use when installing package JUST for this particular project or use case, not for constant use in all/most projects.
>npm install the-package-you-want-without-version-number-if-you-want-the-latest-version
This will create a node_modules folders in you current dir
And inside will be your new package folder
(you may get errors when installing. For example:
>npm install bcrypt
>error: Python 3.4.1 not supported (reqs 2.5-3.0)
So, install Python 2 and then rerun:
>npm install bcrypt --python=python2
This works.
)
INSTALL GLOBAL PACKAGE
Use when installing package for ALL/MOST projects and not JUST for this particular project or use case.
For example: installing http-server to use with multiple projects. You would want to install this package globally.
>npm install http-server -g
*There is no package.json file for globally installed packages
KEEPING TRACK OF PACKAGES AND VERSIONS - Managing package dependencies
package.json - This file is what npm uses to manage all the dependencies for the project
Use npm init to create the package.json file
>npm init
package.json file created
To install more packages after the package.json file has been created, just install them with the --save flag to add them to the dependency list in the package.json file.
>npm instll colors --save
USING NPM-INSTALLED PACKAGES IN YOUR APP
var bcrypt = require('bcrypt');
var colors = require('colors');
Each package file has its own package.json file and in it it tells the file path of the package when using 'require'.
RUNNING APPS THAT USE NPM
>node app.js
USING GIT
You don't want to include packages that have been installed by npm in your git repo since you won't be editing these files anyways.
So, create or add to a .gitignore file
node_modules/
CLONING A GITHUB PROJECT DOWN TO YOUR LOCAL COMPUTER
This clone won't have the node_modules folder since it is standard practice not to include it. But you should have the package.json file.
>npm install
This will look at the package.json file and download and install all of the depencencies.
DEVELOPMENT or DEV DEPENDENCY
packages not needed for the app to run but that you use during development
Exanples: test framework (Mocha)
>npm install mocha --save-dev
This will install Mocha and list the dependency as 'dev' in package.json
If you need to run npm install but just for production and not include the dev dependencies you can set an ENV variable:
>NODE_ENV=production npm install
Mocha basics
Mocha includes a 'test' command and is added to package.json under "scripts"
You can replace "echo \"Error: no test specified\" && exit 1" with simply "mocha"
Mocha requires you to create new folder 'test' in the root of the project.
UPDATING PACKAGES
"dependencies": {
"bcrypt": "^1.0.3", // will install 1.1, 1.2...but not 2.0.0
"colors": "~1.1.2" // will install 1.1.3, 1.1.4...but not 1.2.0
},
1.0.3 -> symantic versioning, symver
1 = Major release. Ver 2 will not work with ver 1, they are incompatible
0 = Minor release. Ver 2 should work with ver 1, they should be compatible. Not guarenteed.
3 = Patch release for bug fixes. Compatible. Good idea to always have the latest patch release.
^ = Tells npm to install minor releases all the way until but not including the next major release.
~ = Tells npm to install patch releases all the way until but not including the next minor release.
blank = Install only that specific version, no updates.
FIND OUT IF PACKAGES ARE OUTDATED
>npm outdated
Then run npm update to update any outdated packages
>npm update
To update gloabally installed packages:
>npm update http-server -g
CAVEAT:
Do Not Run: npm update -g
It will try to update ALL global packages on the system to new major releases and may cause problems
To update npm itself:
>npm update npm -g
UNINSTALLING PACKAGES
>npm uninstall colors
This will not delete the entry for the package in the package.json file. To do so:
>npm uninstall colors --save
or
>npm uninstall mocha --save-dev
To uninstall a global package
>npm uninstall http-server -g