Exercise 4 - Setting Up Node - Js and NPM
Exercise 4 - Setting Up Node - Js and NPM
js and NPM
Note: Make sure you have installed Git on your machine before you install Node.js.
Please complete the previous Git installation exercise before proceeding with this
exercise.
In this exercise, you will learn to set up the Node.js environment, a popular
Javascript based server framework, and node package manager (NPM) on your
machine. To learn more about NodeJS, you can visit https://nodejs.org. For this
course, you just need to install Node.js on your machine and make use of it for
running some front-end tools. You will learn more about the server-side support
using Node.js in a subsequent course. At the end of this exercise, you will be able
to:
Verify that the installation was successful and your machine is ready for using
Node.js and NPM.
Installing Node
As an example, on a Mac, you will see the following web page. Click on the
Download button. Follow along the instructions to install Node on your machine.
(Note: Now Node gives you the option of installing a mature and dependable
LTS version and a more newer stable version. You should to install the LTS
version. I will use this version in the course.)
1
Note: On Windows machines, you may need to configure your PATH
environmental variable in case you forgot to turn on the add to PATH during
the installation steps.
Open a terminal window on your machine. If you are using a Windows machine,
open a cmd window or PowerShell window with admin privileges.
To ensure that your NodeJS setup is working correctly, type the following at the
command prompt to check for the version of Node and NPM
2
node -v
npm -v
Conclusions
At the end of this exercise, your machine is now ready with the Node installed for
further development. We will examine web development tools next.
In this exercise you will learn the basics of Node and NPM. At the end of this
exercise, you will be able to:
Set up package.json file in the project folder for configuring your Node and
NPM for this project
Initializing package.json
npm init
Follow along the prompts and answer the questions as follows: accept the
default values for most of the entries, except set the entry point to index.html
3
Installing an NPM Module
Install an NPM module, lite-server, that allows you to run a Node.js based
development web server and serve up your project files. To do this, type the
following at the prompt:
npm install lite-server --save-dev
Next, open package.json in your editor and modify it as shown below. Note the
addition of two lines, line 7 and line 9.
{
"name": "git-test",
"version": "1.0.0",
"description": "This is the Git and Node basic learning project",
"main": "index.html",
"scripts": {
"start": "npm run lite",
"test": "echo \"Error: no test specified\" && exit 1",
"lite": "lite-server"
},
"repository": {
"type": "git",
"url": "git+https://[email protected]/jogesh_k_muppala/git-
test.git"
},
"author": "",
"license": "ISC",
"homepage": "https://bitbucket.org/jogesh_k_muppala/git-test#readme",
"devDependencies": {
4
"lite-server": "^2.2.2"
}
}
Next, start the development server by typing the following at the prompt:
npm start
If you now open the index.html page in an editor and make changes and save,
the browser should immediately refresh to reflect the changes.
Setting up .gitignore
Next, create a file in your project directory named .gitignore (Note: the name
starts with a period)Then, add the following to the .gitignore file
node_modules
Then do a git commit and push the changes to the online repository. You will
note that the node_modules folder will not be added to the commit, and will not
be uploaded to the repository.
Conclusions
In this exercise you learnt to set up package.json, install a npm package and start a
development server.