Node and NPM

Setup Node JS and NPM for JavaScript Project – 2016

What is Node JS and NPM

..Node.js is an open-source, cross-platform runtime environment for developing server-side Web applications. Although Node.js is not a JavaScript framework,[3] many of its basic modules are written in JavaScript, and developers can write new modules in JavaScript. –wikipedia.com

Node.js is also written as NodeJS,Node JSor just Node. I will try to keep it the wording uniform but please forgive me if I use different variations.

Node JS runtime environment interprets JavaScript via Google V8 JavaScript engine. What makes Node JS even more attractive for back-end server development is the huge ecosystem of Node JS libraries and applications which can be implemented by other developers.

node js

Node JS installation comes with a command line utility called NPM (node package manager) which allows you to easily install, version-control, and manage dependencies of Node JS libraries (or packages), and applications, from the vast NPM online repository. NPM CLI is much like APT, only for Node Packages. Node packages are other JavaScript modules that have been created by other developers, which can be included in your own code to simplify the coding. Some common NPM packages include, Express, Angular, Jquery, etc. (visit https://www.npmjs.com/ for a complete list)

If you have ever done any JavaScript programming, you know how important Node JS and NPM are, however I am assuming you are most-likely an aspiring new developer just learning about Node JS and its capabilities. Node JS allows us to use JavaScript as a back-end server language (much like PHP). What makes it even more attractive is the large quantity of packages (or libraries) available via it’s command line utility (cli), NPM. The possibility of being able to use JS on the back-end as well as the front-end helps building, and managing scalable applications much easier through the use of npm packages.

You should also have NPM installed on your Kali/Debain machine.

Install Node JS and NPM

Node JS is available for the following:
WindowsLinux

Linux Setup

You may be able to install Node JS and NPM via APT (Advanced Packaging Tool) depending on your sources list or Kali Linux version (If you have the latest version of Kali the following commands should work). After Debian 8, running the following commands in the Terminal should take care of the install:

apt-get install nodejs
apt-get install npm

If you are unable to get the install to work, I found a great tutorial over at tutorialsforlinux.com .

Another great resource for getting up and running with Node JS and NPM is available here: https://nodejs.org/ .

Windows Setup

Installation for Windows is as easy as downloading the Node JS exe from the download page, and running the setup file. This will take care of installing Node JS and Node Package Manager (NPM) globally.

Globally means you should be able to run the npm command anywhere from your Windows or Linux file system.

To check which version of Node or NPM you have, run the following commands in the Terminal or Windows Command Prompt:

node -version
npm -version

Search for packages using NPM

node js npm
Now that you have Node JS and Node Package Manager (NPM) installed on your machine, you should be able to easily download “node modules” and add them to your projects. To search for the packages available on NPM, you can visit the NPM website, or open Terminal and run:

npm search [package name]

You should then be given a list and short description of any packages that match. Browse through the list and find the package you are looking for. To install, run the following from your project directory:

npm install [package name]

This will create a folder named node_modules inside your project directory, and place your package inside it. After that, you should be able to utilize the module/package from your JavaScript application.

Using the “-g” option will install the package globally. For example, the following command will install the Express module globally, so I can use the express generator command anywhere from my file system:

npm install express-generator -g

After Express installs, I can run express myExpressApp (from whichever directory in my Terminal/Command Prompt) and it will create a folder named myExpressApp with a basic template of files and folder structure (routes, views, etc.) to get you started quickly. For more on Express please visit their website http://expressjs.com.

Using Node JS and NPM in your JS Project

From your project directory (e.g. c:\myNodeProject\), run npm init. This will walk you through the steps of setting up your node js application. Simply answer the questions it asks, you can skip most of them buy just hitting [ENTER]. When you are finished answering all the questions, you should have a package.json file. The package.json file helps manage back-end packages/dependencies for your Node JavaScript project. It will contain all the node modules in your node_modules folder.

OR

You can create a package.json file first, and list all the packages/dependencies that your application will require. Then run npm install and it will go through and install all the packages listed in the file. Make sure you use the correct structure for the package.json file. Just open up another .json file and copy the structure.

You can also append more dependencies to your package.json file by using --save flag at the end of the install command, for example:

npm install jquery --save

This will add to the existing list of dependencies in the package.json file.

Afterwards, create a file called nodetest.js and save it in your project directory (c:\myNodeProject). Open up nodetest.js in your favorite text editor, and type the following line:

console.log ("This is a Node Test.")

Save the file and run it by using the node nodetest.js from the Terminal/Windows Command Line. So, your output should look like:

This is a Node Test.

Summary

In conclusion, Node JS is very useful when creating JavaScript applications. It allows you to use code written by other developers as well as your own, for server-side development. When you write a Node JS application, you are technically configuring a web server to serve your static or dynamic content. It can minimize, if not eliminate, other languages such as PHP (although I am sure you will run into PHP if you are looking into getting into web-development 🙂 ). Node JS is easier to learn and setup for developers already familiar with JavaScript or beginners looking to create their own dynamic, scalable, JavaScript website.

Now that you have Node JS and NPM setup on your machine, I recommend working through some basic tutorials:

  • TechSide Node JS Server my tutorial on how to create your first simplified Node JS Server and serve static HTML pages. It shows you how to serve a static homepage page and handle 404 errors using routing.
  • NodeJs.org also has some documents to help get you started.
  • NPMJS.com also has some great documentation and list of available Node packages to include in your NodeJS Projects.

If you’re just getting into programming, I recommend setting up an IDE (Integratged Development Enviroment) as it will help keep all your code and files organized. I like to use Sublime Text 3 as an IDE, but others such as WebStorm, PSPad, Eclipse and NetBeans can also be used (just to name a few).

Leave a Reply

Your email address will not be published. Required fields are marked *