1.4. Updating Node.js and npm

It is really important to learn about some Node.js development tools if you are planning to become an effective TypeScript developer as many of the Node.js tools are also intensively used during the development of TypeScript applications. The Node.js installation will also install the node package manager (npm) automatically. After some weeks or months since the last Node.js installation in your development environment you may encounter some issues while installing new npm packages because your version of npm or Node.js is outdated. In this recipe, you will learn how to upgrade both Node.js and npm without having to manually visit the Node.js downloads page to download and execute the installer. Both Node.js and npm are available as npm packages, which means that you can upgrade these tools using the npm command line interface. Usually there are more releases of npm than Node.js so we will start by learning how to upgrade npm.

How to do it…

  1. To upgrade npm in your development environment you must execute the following command:
    $ npm install npm –g
    

    As you can see, the command is just installing the npm module as a global module.

  2. You can also use npm to upgrade Node.js:
    $ npm cache clean -f
    $ npm install -g n
    $ n stable
    

    If you are working on OSX or Linux, you will be required to run the preceding commands using administrator privileges. You can do so by preceding the previous commands with the sudo command. If you are expecting to work on multiple projects that require a specific Node.js version, you can use an open-source tool known as node version manager (NVM). Unfortunately, NVM does not support Windows. However, there are a couple of alternatives available for windows users. Please refer to https://github.com/coreybutler/nvm-windows and https://github.com/hakobera/nvmw for more information. OSX or Linux users can install NVM using the following command:

    $ curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.2/install.sh | bash
    
  3. Let’s use the following command to download, compile, and install the v5.0.x release of Node.js:
    $ nvm install 7.10.0
    

    You can switch to a specific version of Node.js using the following:

    $ nvm use 7.10.0
    
  4. You can also execute a Node.js application using a specific version of Node.js without switch to that version using the following command:
    $ nvm exec 7.10.0 node –version
    
  5. You can find it the path to the Node.js executable of a specific version using the following command:
    $ nvm which 7.10.0
    

See also

Please refer to the NVM documentation at https://github.com/creationix/nvm for more information.


Shiv Kushwaha

Author/Programmer