knowledge

Xpeho knowledge database

View project on GitHub

NodeJs

logo

What is NodeJs?

Node.js is an open-source, cross-platform, JavaScript runtime environment that executes JavaScript code outside a web browser.

Installation

First download the latest version of NodeJs from here.

We recommend to use the LTS version (for Long Term Support).

Usage

Hello World

// let's create a file called hello.js
console.log("Hello World!");

Run the file

# open a terminal and run the following command
node hello.js
# you should see the following output
> Hello World!

Dependencies

NodeJs comes with a package manager called npm (Node Package Manager).

You can install dependencies using the following command:

# install a package locally
npm install <package-name>
# install a package globally
npm install -g <package-name>

Package.json

The package.json file is used to store metadata about your project.

You can create a package.json file using the following command:

npm init

This will create a package.json file in the current directory with the following content

{
  "name": "my-project", -> the name of your project
  "version": "1.0.0",
  "description": "My Project Description",
  "main": "index.js", -> the entry point of your project
  "scripts": { -> scripts to run
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "dependencies": { -> dependency list
    "dependency-name": "^1.0.0",
    "other-dependency-name": "^1.0.0"
  },
  "author": "Your Name",
  "license": "ISC"
}

Full Documentation

You can find the official documentation here.

Resources