#Intro to Node (part 2)
title: more intro to Node type: lesson duration: '"1"' creator: name: Blaise Thomas city: Los Angeles competencies: Server Applications
Note to instructors: This lesson serves as a bridge between "Intro to Node" and "Debugging and logging". It covers some minor concepts of running node and npm, the package.json and why node_modules can be git ignored. It was thrown together in 10 minutes and is full of typos and general errors. PRs welcome.
###Objectives
- npm init
- package.json
- entry point
- nodemon & global installs
npm install xxx
--save
- dependencies : node_modules
- .gitignore review
###Preparation
- Use module.exports and require to organize code
This starts a new node project. As we learned earlier we can run node against any Javascript file. npm init
creates a package.json file. This is the summary of our entire project. It is where we will store all sorts of information about dependencies, entry point, test suite etc. This is not a necesary step; you can run node against any JS file, however working in this way will make things easier.
####Try it!
-
mkdir test_node_project
-
npm init
creates a package.json file. This generator will walk you through a step by step in the command line to configure this file. Accept all the defaults this time (yes yes yes yes yes yes) -
subl .
Now go inspect the package.json file.
Note: if you want to accept all default settings on npm init
you can use -f
=> npm init -f
This is a default package.json file:
{
"name": "test_node_project",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "Blaise Thomas",
"license": "ISC"
}
The two main areas to focus on are "main" and "dependencies" (one created)
####Try it!
touch index.js
- open it in sublime
- make it console log "HELLO WORLD"
- run the file '$ node index.js'
Looks like index.js is our entry point to our application. This will make more sense when we start adding more code, because in future when we have dozens of JS files, and because JS is single threaded (can only run one line at a time), we need to tell the V8 engine where to start running our code. If our app is nicely setup everything will snowball from here.
Note you can name your entry point whatever you like. Typically in Express projects you will see this be named app.js or server.js
npm install -g nodemon
-g
is a global install and will be available to all node projects on your system. This is different to installing dependencies which we will look at in a sec. You will only need to run the above command once on this computer!
This is a really cool (really simple) utility that basically looks at the package.json and runs the value of the "main" key as the starter script, or entry point.
It also watches for changes.
####Try it!
you can now run $ nodemon
instead of $ node index.js
notice that this time we are hanging and waiting for changes to index.js.
You can ctrl + c
to exit.
Try adding another line of code to Hello World and watch nodemon detect the change, and re-run the whole file.
Dependencies are the core of how node works. We learned how to make our own modules, but npm also has a bunch of existing modules, authored by third parties, that we can leverage. NPM cleverly manages these dependencies for us and manages how they are sourced in. These modules are very similar to gems.
####Try it!
npm install morgan
This creates a new directory called node_modules. We will almost never make ANY changes to anything inside this file. Don't worry about exactly what morgan is doing for us just yet.
####Q: why do we not cahnge node_modules?
####Q: is this a global install??
we will learn what morgan does in a sec, and we will learn all about express tomorrow, but just for giggles lets:
npm install express
Notice through all of this that the dependencies section of our package.json has NOT changed.
So from now on, we will almost always do project specific dependency installs using
npm install xxxdependency-name-found-in-npm-libraryxxx --save
####Try it!
eg:
npm install morgan --save
npm install express --save
Notice that npm doesn't reinstall extra copies of express and morgan to our node_modules, it just updates our package.json (because they were already there!)
In future when you clone down other peoples apps, you will have JUST their package.json and code files specific to their projects. You will get all the dependencies they are using by doing an npm intall
####Try it!
Delete your node_modules directory and then run npm install
. What happened?
We have already looked at gitignore, but a quick refresher.
####Q: What does this file do?
because we will NEVER make changes to our dependencies, and we will ALWAYS get these files from npm, the we can add the entire contents of the node_modules directory to gitignore
create a .gitignore
and add the /node_modules
directory
$ git status
- try removing
/node_modules
from.gitignore
and do a git status again! - yup! its as easy as that!
##Conclusion
- npm init - creates a new package.json
- package.json - a place to configure how our app runs, and manage dependencies
- entry point - aka "main" - the starting point
- nodemon & global install - running node programs that help us to do better programming!
npm install xxx
- installing dependencies in our apps--save
- making sure those dependencies get listed in our package.json
- dependencies : node_modules - the tools we will use to build our apps
- .gitignore (review)