Install Node.js
Node.js installation is a simple task. We just need the right steps. In this tutorial, we shall learn to setup environment for Node.js and the prerequisites to start developing Node.js applications.
JavaScript programming language is used for Node.js Application Development. The source files of Node.js applications have extension of “.js”. Any text editor is sufficient to write Node.js code and save it as .js file.
To interpret the instructions written in the source .js files, Node.js interpreter is required. The Node.js is available as a binary distribution.
Download Node.js
Download the latest binary distribution from [https://nodejs.org/download/release/latest/]. Based on the Operating System and Architecture, download a suitable package. The size would be less than 20MB.
Following are the latest packages available at the time of writing this tutorials (for x64).
OS | Package |
Linux | node-v8.4.0-linux-x64.tar.gz |
MacOS | node-v8.4.0-darwin-x64.tar.gz |
Windows | node-v8.4.0-win-x64.zip |
SunOS | node-v8.4.0-sunos-x64.tar.gz |
For linux kernel based OS like Ubuntu/MacOS/SunOS
Once the download is complete, extract the package and include it in the system path variable.
Go to the downloaded folder, Open a Terminal from there and execute the following commands.
Unzip the compressed package
~$ tar xvfz node-v8.4.0-linux-x64.tar.gz
Make nodejs directory in /usr/local/ . Replace the file name, if it is different from what you have downloaded.
~$ sudo mkdir -p /usr/local/nodejs
Move the extracted nodejs package to /usr/local/nodejs/ . Provide user password if asked.
~$ sudo mv node-v8.4.0-linux-x64/* /usr/local/nodejs/
Add the path /usr/local/nodejs/bin to PATH environment variable. Provide user password if asked.
Open .bashrc file and append the nodejs’ path at the end. To edit .bashrc, open a terminal and run the following command :
Add the following line at the end of .bashrc file.
$ export PATH=$PATH:/usr/local/nodejs/bin
Once after adding the line, close the terminal and reopen again.
To verify if nodejs path has been added to PATH environment variable, run the following command :
For Windows OS, the value echoed back should have /usr/local/nodejs/bin .
For installing Node.js on Windows Operating System, double click on the .msi file and follow the prompt step by step. Finishing the prompt should install Node.js with PATH added to the environment variables.
Verify Node.js installation
Using a Text Editor, create a sample file with name verifyNode.js and copy paste the following content to that file.
verifyNode.js
console.log("Hi there! This is Node.js!")
And run the following command in Command prompt or Terminal from the directory of verifyNode.js script file.
$ node verifyNode.js Hi there! This is Node.js!
Run Node.js File
Following is the syntax to run a Node.js file using node command line interface :
Conclusion
In this Node.js Tutorial, we have learnt to install Node.js on a computer and verify the installation by executing a sample script(.js) file.
how can I find my node.js files in linux, /usr/bin/node is not working
I want to find where is my node.js in ubuntu linux system, the command: which node gives me the path of /usr/bin/node, but when I go to that folder, there is no node folder and files. can somebody help me? I installed the node.js by this:
sudo apt-get install python-software-properties python g++ make sudo add-apt-repository ppa:chris-lea/node.js sudo apt-get update sudo apt-get install nodejs
5 Answers 5
In order to find the installation path, write the below command in the terminal:
If it doesn’t succeed, try this one:
Same thing for finding npm installation path:
If you are on Windows, write where instead of which
running dpkg-query -L nodejs will list the full path to every file belonging to the nodejs package. If /usr/bin/node is not there (it should be a symlink to /usr/bin/nodejs ), then something went wrong with the apt-get install .
The question is asking about the Ubuntu linux distribution specifically. dpkg-query should be in the base package set and always installed, I believe. If you don’t have it at /usr/bin/dpkg-query you are probably not on a debian/ubuntu based distribution.
don’t worry sudo apt-get install nodejs installs a version of nodejs which is normally outdated. /usr/bin/nodejs is therefore fine.
Do some additional work to use only node for the command line:
- install package manager npm: sudo apt-get install npm
- then upgrade npm: sudo npm cache clear —force && sudo npm install -g npm
- next install n: sudo npm install -g n which is a version manager for node.
- after this upgrade your node installation: sudo n stable this will create a /usr/bin/node script which fixes your described issue so you can use node app.js to execute your app instead of nodejs app.js .
You can downgrade node to a desired version, e.g: sudo n 0.12.4
check your version to verify: node —version
If you have both Nodejs and npm installed correctly, just open your terminal:
Run: npm config ls -l to see a set of configuration parameters that are internal to npm.
npm is configured from the following sources, sorted by priority:
- Command Line Flags: —save-dev, —prefix, —global
- Environment Variables: npm_config_foo=bar or NPM_CONFIG_FOO=bar
- Both are correct values but just know that inside npm-scripts npm will set its own environment variables and Node will prefer those lowercase versions over any uppercase ones you set.
- Also you need to use underscores instead of dashes, so —allow-same-version would be npm_config_allow_same_version=true
- per-project: /path/to/my/project/.npmrc
- per-user defaults to:( $HOME/.npmrc ; also configurable via CLI option —userconfig or environment variable $NPM_CONFIG_USERCONFIG )
- global defaults to:( $PREFIX/etc/npmrc ; also configurable via CLI option —globalconfig or environment variable $NPM_CONFIG_GLOBALCONFIG )
- npm built-in configuration file: ( /path/to/npm/npmrc )
For those who may be unfamiliar or new to Nodejs, npm and nvm the user needs to be aware that it’s possible to have more then one version of Node on your system.
It’s also possible to have Node stored both locally and globally.
With multiple versions and different locations it’s possible that $ which node may not give you the right location and if you run $ locate node your gonna end up with too many locations to sort through.
Using the built-in Node/npm tools to locate Node seems to make the most sense.
How to set node path for nodejs (Ubuntu)
I’m trying to setup nodejs to access a postgres database. What I’ve done so far is the following (https://gist.github.com/579814):
echo 'export PATH=$HOME/local/bin:$PATH' >> ~/.bashrc . ~/.bashrc mkdir ~/local mkdir ~/node-latest-install cd ~/node-latest-install curl http://nodejs.org/dist/node-latest.tar.gz | tar xz --strip-components=1 ./configure --prefix=~/local make install # ok, fine, this step probably takes more than 30 seconds. curl http://npmjs.org/install.sh | sh
git clone git://github.com/isaacs/npm.git make make install
Checking for program g++ or c++ : /usr/bin/g++ Checking for program cpp : /usr/bin/cpp Checking for program ar : /usr/bin/ar Checking for program ranlib : /usr/bin/ranlib Checking for g++ : ok Checking for node path : not found Checking for node prefix : ok /usr/local Checking for program pg_config : /usr/bin/pg_config 'configure' finished successfully (0.066s) Waf: Entering directory `/home/christian/node_modules/pg/build' [1/2] cxx: src/binding.cc -> build/default/src/binding_1.o ../src/binding.cc:3:25: fatal error: node_events.h: No such file or directory compilation terminated. Waf: Leaving directory `/home/christian/node_modules/pg/build' Build failed: -> task failed (err #1): binding_1.o>
I’ve been looking around for setting the node path, although haven’t found anything of help so far — probably also because I’m totally new to nodejs, so I’d be happy about any hint.