Understanding and Installing Node.js and npm

Node.js become one of the most popular JavaScript-driven technologies in short time and even the enterprise corporation have started using Node.js in their production environments. Since it was created 2008 by Ryan Dahl, the framework has evolved into a well-developed ecosystem with thousands of useful modules that can easily be integrated with package manager.

Differences from Other languages

Unlike the modern world multithreading server languages such as Java or C, Node.js is a single-threaded architecture. However, Node.js is highly scalable by running different Node.js processes using a load balancer.

Components

Basically, Node.js is consisted of three things:
1. Google's JavaScript engine called V8, used in the Chrome
2. The event loop library and
3. A thread pool that handles all file input/output operations
Upon them, a bundle of several bindings layers exists that exposes low-level interfaces to the Standard Node Library, which is written in JavaScript.

Installing (Mac OS X)

  1. Download the Node.js Mac OS X installer from its official website https://nodejs.org. The latest stable version is v5.9.1 in the time of writing this post.
  2. Open the installer from downloaded .pkg file and Click Continue and Agree when prompted some messages as usual as other app installers.Nodejs Installation first screenshot

  3. Type your admin-level password when prompted. If not, please ignore this step.

  4. When Node.js is finished installing, you will see this screen that showing where Node.js and npm are installed.Nodejs Installation Success

  5. You will also see a reminder to make sure that /usr/local/bin is in your $PATH. You can check your $PATH by typing echo $PATH in your Terminal. If /usr/local/bin is included, you are fine and ready to use your npm commands in your Terminal in any working directory. If not, don't worry, you can see at Troubleshooting section in this post below how to fix.

  6. To test whether Node.js is installed on your machine, type node --version or node -v in your Terminal. It will show the version of the Node.js that your installed.
    Nodejs node version and node version

What is Node.js & npm

Great, you have installed the most popular framework in your machine. But what is npm doing while I am installing Node.js?

Node.js is a command-line tool. After installing it, the node commands will be available on Terminal. These commands comes with two executables in mainly - node and npm.

In Node.js world, the building blocks that contained groups of programming or business logic, are considered as modules or packages. This is where npm comes in order to download and upload or manage these Node.js packages. The official site for npm acts as a central registry and you can see there are a lot of useful packages written by other developer. You can also create and register one via npm command as well to there.

Troubleshooting

Including the /usr/local/bin in your $PATH
  1. Open Terminal.
  2. Type cd ~ and this will bring you to home directory.
  3. Type sudo vi .bash-login.
  4. Type your admin-level password.
  5. Then you will see a [New File] or an existing one named .bash_login will be look like as below. Create bash file

  6. Type i to bring insert task.

  7. If the .bash_login file is new,
    • Type PATH=$PATH:/usr/local/bin:.
  8. If the file already has previous path variables,
    • Just append :/usr/local/bin: at the end of the existing PATH statement.
  9. Hit Escape to quit editing or inserting.
  10. Type :wq [write and quit] and press Return key.
  11. Type exit in Terminal and quit.
  12. Open the Terminal again and type echo $PATH to verify your path.

Nay Win Myint

A JavaScript full-stack web developer, also interested in Android application development and Graphic design.

United Kingdom