NPM intro
November 21, 2016
NPM
- npm = Node Package Manager
- Let's you install and manage your modules. A module is just a bunch of code grouped together.
- It makes it easy to update your modules (versions) without having to edit code.
why NPM?
- makes it easy to download different modules
- makes it easy to upgrade different modules (e.g. jquery, bootstrap)
- makes it easy to remove modules
- it's a package manager. it makes your life easy by making managing your modules and packages easier
- it let's you install modules with single commands instead of having to download code locally and including it, or including links to CDNs.
getting started
npm comes with Node.js. If you have Node installed, you have npm installed. You can check if it's already installed with
code
node -v
npm -v
- install npm:
code
# Install on Ubuntu Debian
sudo apt update && sudo apt install nodejs npm -y
- initialize:
code
npm init
inside your project folder. will create package.json
- install modules:
code
npm i -S jquery
npm i -D react react-dom
iis short forinstall--saveor-Sadds it to package.json as a dependency--save-devor-Dadds it as a dev dependency.- Without
-Sor-D, jQuery would have been installed into our node_modules/ folder but not saved to the package.json file.
installing specified dependencies in package.json
if you were collaborating with a teammate, instead of having to push your entire node_modules folder to github, your teammate can just clone your project and run npm install and all of your dependencies that are saved inside of your package.json file (in this case just jquery) will be downloaded to her node_modules folder.
NPM Scripts
let's you run commands you saved under scripts in package.json.
Inside of your package.json file, under the "scripts" property, add
code
"test": "ava 'app/**/*.test.js' --verbose --require ./other/setup-ava-tests.js"
so that your complete scripts sections looks like
code
"scripts": {
"test": "ava 'app/**/*.test.js' --verbose --require ./other/setup-ava-tests.js"
}