Exercise 2: Installing Packages and Using Packages with Node.js
Learning Objectives
- install a package using npm
- use package in a Node.js program using
require
Prerequisites
- you should have Node.js >= v4 installed
- you should have npm v2 or v3 installed
Instructions
- Create a directory called
my-project
- Change into that directory using
cd my-project
- Install the
my-first-mod
- Create a file called
index.js
- Use the
my-first-mod
to add the numbers 3 and 4
- Use the
my-first-mod
to have the console print Hello, <your name>!
Hints
- You can use
http://npmjs.com
to look at the README
of a package.
A README
will often give you information on how to use the package.
- You can use
console.log(<string>)
to print things to the console
with Node.js, where <string>
is a set of characters or a variable
that contains a set of characters
- You can run a Node.js program by typing
node <js-file-name>
where
js-file-name
is the name of a .js
file, e.g. index
, index.js