What is Node JS and NPM
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.
You should also have NPM installed on your Kali/Debain machine.
Install Node JS and NPM
Node JS is available for the following:
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/ .
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
npmcommand 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
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
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
myExpressAppwith 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
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.jsonfile by using
--saveflag at the end of the install command, for example:npm install jquery --save
This will add to the existing list of dependencies in the
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.
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).