How to Update NPM Packages

28/12/2020

Updating npm packages in Ubuntu 16.04

Node Package Manager, or npm, gets installed along with Node.js and you can use it to import software packages built on top of Node.js.

If you are not familiar with the idea of nodejs, it is based off of Google Chrome’s JavaScript engine and can be used for server side scripting using JavaScript. It brings JavaScript (which traditionally runs on the client e.g a web browser) at the same footing as any other server side language like .NET or php.

Because the language is already very popular and easy to use. There are now a myriad of applications built on top of the Node.js to the point that it almost has its own ecosystem of applications and frameworks to build applications each bundled as its own package. The problem with that arises when developers start to improve upon their packages and releases new version.

Like most package managers, npm installs the most recent (stable) release of a package. So if you install a particular version of Express for your web application and a few years later after a new version of Express, chances are somethings in your app might break in a direct or indirect way.

To circumvent this issue, it is handy to use npm in a way that makes use of version numbers to keep track of packages.

Installing Node.js and npm

If you don’t already have Node.js installed in your system it is a good idea to start with the official LTS release. The official repository of Ubuntu 16.04 is a bit behind from the latest stable release and we would thus add the Node.js official repo as a PPA.

$curl sL https://deb.nodesource.com/setup_8.x | sudo -E bash –

The above command fetches a shell script from deb.nodesource.com and runs it via bash. The script automatically adds the public keys of the repo and updates the source lists for apt package manager.

After this installing Node.js and npm is as easy as running:

$apt install nodejs

Now before we get started with installing and updating various packages, let’s make sure that the version of npm and Node.js is what we desire them to be.

$nodejs version
$npm version

The version of npm is not the latest stable release (although we made sure that Node.js is  the LTS version)

To update npm itself you can run the command:

$sudo npm install npm@latest -g

Where the -g flag makes sure that the package is installed globally, that is, for all the users. You can only do this if you are the root user or have root previleges of the system. After which you can see that the version number for npm has changed.

If you would like to go back you can enter the previous version number in a similar fashion. For example:

$sudo npm install npm@5.6.0 -g

Installing and updating local packages

npm offers 2 different methods for installing packages. First is locally to be used in, say, another software that you are trying to build and the second method for installing it is to do it across the system for all users. This is great if you are trying to install a system utility, say with a CLI interface, to be used as a basic command or a standalone software.

The local packages are directory specific. For example, if you are building a Node.js app first make project folder or directory:

$mkdir myapp
$cd myapp

Now, from inside the directory you can run an npm init command and enter appropriate values for the name, description, git repository and other relevant fields which are the metadata of the package.

Now if you install a package, such as lodash by running the following command in the directory:

$npm install lodash

npm automatically gets the latest stable release of the said package and installs it for you.

If you want to check the version number then, enter the command:

$npm list

If a new version of lodash comes along and you wish to upgrade then, in the same directory, run:

$npm update

Now you can see that the version number is greater than what it was before.

If something is wrong with the new version and you are facing issues, then you can always go back to previous state by simply running.

$npm install lodash@4.17.0

Of course, that would require keeping track of the previous version number. Performing a git commit before any of the npm update actions is advisable for this reason.

Installing and updating global packages

Installing packages globally is actually much simpler since these are usually standalone utility. A great example of such a package is gtop which is similar to the top  utility in Linux. It shows all the resource utilization in a clean and elegant way.

To install a package globally, either be the root user or prefix your command with sudo if you are a user with root previleges.

$sudo npm install gtop -g

Now, like any command utility, you can call upon gtop by running:

$gtop

To exit simple hit q and you will fall back to the terminal. Because there are too many dependencies for gtop simply running npm list -g won’t help us know the verison of gtop. So we can try this instead:

$npm list gtop -g

If you want to update the package,then simply run:

$sudo npm update gtop -g

And you are done!

Conclusion

So that’s basic package management if you want to use npm packages for your new software project or as a standalone system utility. If you wish to explore npm packages you can always do so by going to their official website.

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

PostgreSQL NodeJS Tutorial

Using PostgreSQL from Node.js PostgreSQL is a very popular open source database. It is widely used for almost all types...
28/12/2020

Setting Up Node.js Development Environment

You can always install Node.js on your computer and run Node.js applications there. But, there are situations where you...
29/12/2020

What is NodeJS?

In this tutorial, we will get started with Node.js. Learn about some of its most popular features and will try to motivate...
28/12/2020