What is Node.js?

Server-side JavaScript.

An open-source, cross-platform JavaScript run-time environment that executes JavaScript code server-side.

Like

, Node runs on 

What is npm?

  • World’s largest software registry (600,000+ packages)

  • CLI comes bundled with Node.js

  • Install and keep track of dependencies (external packages/modules) for all of your Node.js projects

  • Register with npm to publish open-source packages

Why Node.js?

  • Unified API - frontend and backend can use JavaScript. Modern frontends often use Node.js (via server-side rendering or as a build tool).

  • NPM is included standard. It has many isomorphic packages (use them on both frontend and backend), and it is fairly simple to use.

  • Good with JSON. Most RESTful APIs use the JSON format. Node has native handling of JSON (obviously).

  • Decent Performance. It’s good enough for many uses.

  • “Non-blocking I/O” is pretty cool.

Starting a Node.js Project

In the root project folder, from the terminal run:

 

touch index.js
npm init

 

Then hit enter for all of the default prompts and type “yes” at the end.

 

npm init --yes

shorthand

This generates a package.json file which serves to list dependencies, project metadata, and can be used to execute scripts.

Example package.json

{
  "name": "hack-or-snooze-api",
  "version": "1.1.0",
  "description": "Hacker News API Clone",
  "main": "NODE_ENV=development nodemon server/server.js",
  "scripts": {
    "preinstall": "npm install pm2 -g",
    "start": "NODE_ENV=production pm2 start server/server.js -i 2 --attach",
    "dev": "NODE_ENV=development nodemon server/server.js",
    "stop": "pm2 stop all"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/rithmschool/hack-or-snooze-api.git"
  },
  "keywords": ["hackernews", "api", "expressjs", "nodejs"],
  "author": "Michael Hueter",
  "license": "ISC",
  "engines": {
    "node": "8.1.1",
    "npm": "5.6.0"
  },
  "bugs": {
    "url": "https://github.com/rithmschool/hack-or-snooze-api/issues"
  },
  "homepage": "https://github.com/rithmschool/hack-or-snooze-api#readme",
  "devDependencies": {
    "axios": "^0.18.0",
    "eslint": "^4.19.1",
    "nodemon": "1.17.5",
  },
  "dependencies": {
    "bcrypt": "2.0.1",
    "body-parser": "1.18.3",
    "dotenv": "5.0.1",
    "express": "4.16.3"
  }
}

Asynchronicity

Node.js is often called “async by default,” because most library functions (for example, reading a file) are designed to use callbacks.

const fs = require('fs');

fs.writeFile('./test.txt', 'hello world', function(err) {
 if (err) {
   console.error(err);
   return process.exit(1);
 }
 console.log('done');
});

console.log('not done');

Installing a Package

Before we install NPM packages, our project must have a package.json file (created by the npm init command).

  1. Looks up the package and its dependencies in the npm registry
  2. Downloads that content into a node_modules folder

  3. Adds the package name and version to the dependencies object of package.json and to package-lock.json

After this, from the terminal (project directory) we can 

npm install <package>

This does a couple things for us:

node_modules

You never want to commit the node_modules folder to git. It is a massive collection of code dependencies (usually in the MB). It should always be added to a .gitignore file.

Instead, package.json and package-lock.json list dependencies,

and a simple “npm install with no arguments automatically installs everything specified in those files.

Intro to Node.js

By Michael Hueter

Intro to Node.js

An open-source, cross-platform JavaScript run-time environment that executes JavaScript code server-side.

  • 1,071