Using Node.js and Npm packages

Node.js and Npm

Otherwise, After starting with Git, GitHub, and open source projects, you will find a lot of projects using Node.js and Npm packages. Node.js is not only a tool to create web application backends. Also, is used as a development platform for large scale projects to manage dependencies and  perform operations in the project lifecycle. His companion, Npm, is a package manager to make available a large ecosystem of modules and functionalities used in the development stage, in the project itself and in the development pipeline.

Installing Node.js

You can install Node.js in different ways depending on your OS. Npm will be installed along with Node. You can download an official installer. Also, you can  install Node.js using a package manager :

  • MacOS:
    • With HomeBrew
      brew install node
    • Using MacPorts:
      port install nodejs8
  • Debian/Ubuntu
    • For Node.js
      • wget -q0- https://deb.nodesource.com/setup_10.x | sudo -E bash -
      • sudo apt-get install -y nodejs
  • Windows

Checking your installation

Check your current version of Node.js and Npm using the following commands:

node --version

npm --version

Also, you can request an update of Node.js with the following commands:

  • With HomeBrew:
    • brew update
    • brew upgrade node
  • With MacPorts
    • sudo port selfupdate
    • sudo port install nodejs

Using Node.js

As a command line interpreter

You can create and run your own scripts written in javascript in a console. For example, you can create a file named test.js:

console.log("Hello world");

And then run your script using the command

node test.js

Then , the output will be:

Hello world

As a local http server

You can serve local files right from your console using Node http-server module. First of all you need to install the http-server Npm package (-g option is to install it for all users) :

npm install -g http-server 

Then you can serve your files in any folder using the simple command

http-server

By default, it listens in the 8080 port, for example, http://localhost:8080/). Also, you can specify the server port with the -p option and a target home directory:

http-server -p 8000 ./

In this case, its serving your files from the relative ./folder (current) using the port 8000

Users-MacBook-Pro-6:Downloads user$ http-server -p 8000 ./
Starting up http-server, serving ./
Available on:
http://127.0.0.1:8000
http://192.168.1.71:8000
Hit CTRL-C to stop the server

Using Npm

To install global packages

Use the option -g to install any package as global (available for all users)

npm install -g package-name

To setup and manage project dependencies

Use npm init to start a new node package definition. It creates the default npm files and structures, and asks for some basic information about your package. It’s better to setup a npm project after the git initialization, to keep it synchronized with a git repository.

Use npm install to install project dependencies already defined in the project.

Leave a Reply