NodeJS is an open-source, cross-platform runtime environment for developing server-side web applications. NodeJS also has an event-driven architecture capable of asynchronous I/O.
NodeJS uses an event-driven, non-blocking I/O model that makes it lightweight and efficient.
Installation of NodeJS and NPM is straightforward using the installer package available at NodeJS official web site.
Now, test NodeJS by printing its version using the following command in Command Prompt:
node -v
npm -v
Lets create test.js file
/*test.js file*/ console.log("Node is working");
Run the test.js file using Node command node test.js
in command prompt.
You are done with installation.
NPM is the package module which helps javascript developers load dependencies effectively. To load dependencies we just have to run a command in command prompt:
npm install
This command will search for the file package.json
in the directory from where it run to install all dependencies defined in the file.
Package.json looks like:
{ "name": "ApplicationName", "version": "0.0.1", "description": "Application Description", "main": "server.js", "scripts": { "start": "node server.js" }, "repository": { "type": "git", "url": "https://github.com/npm/npm.git" }, "dependencies": { "express": "~3.0.1", "sequelize": "latest", "q": "latest", "tedious": "latest", "angular": "latest", "angular-ui-router": "~0.2.11", "path": "latest", "dat-gui": "latest" } }
The most important things in your package.json are name and version. Those are actually required, and your package won’t install without them. The name and version together form an identifier that is assumed to be completely unique. Changes to the package should come along with changes to the version.
{ "repository": { "type": "git", "url": "https://github.com/npm/npm.git" } }
Specify the place where your code lives. Through this repository, developers can reach out and contribute to your application. If the git repository is not GitHub, then the npm docs
command will be able to find you.
{ "scripts": { "start": "node server.js" } }
NPM provide many useful Scripts like npm install
, npm start
, npm stop
etc.
Some default script values are based on package contents.
"start": "node server.js"
{ "dependencies": { "express": "~3.0.1", "sequelize":"latest", "q":"latest", "tedious":"latest", "angular":"latest", "angular-ui-router": "~0.2.11", "path":"latest", "dat-gui":"latest" } }
Dependencies are specified in a simple object that maps a package name to a version range. Version Name must be Version exactly.
If you want to install the latest version of a file, you just have to put latest
in place of the version name.
Tilde(~) is used to tell “Approximately equivalent to version”.