When building web applications using JavaScript frameworks
like React or Angular, there are tools that enable the installation of these
frameworks and aid in the development process. These tools are essential to be
able to build, test and manage some dependencies in our projects. It is
those package dependencies that we continuously need to keep up with the latest
update. To update those packages, we use the NPM CLI tool, which runs on the
NodeJS runtime environment.
When we need to update a package, we may face issues that a
package or a new version of that package is not supported (see error below) for
the current version of Node.js and that we need to update to a new version. In this
article, we discuss the tools that are used to manage the software development
process and how to best update NodeJS using the command line interface (CLI).
npm WARN notsup Unsupported
engine for create-react-app@5.0.0: wanted:
{"node":">=14"} (current: {"node":"12.16.1","npm":"6.14.4"}) npm WARN notsup Not compatible
with your version of node/npm: create-react-app@5.0.0 |
This error message indicates that a particular required
version of Node.js is not in the system and node version 14 is a dependency.
What is Node.js?
Node.js is a cross-platform JavaScript runtime environment.
It is used by software engineers to build server-side and client-side web applications.
When building client applications with
popular frame works like React, Angular and others, Node.JS provides the
runtime environment for the JavaScript applications to run. It also enables the
build and test tools that are used during the implementation effort of those
applications.
JavaScript's applications are made of several libraries or
packages that can be added to the project. Those libraries are mostly refereed
as packages, and to install them, developers use a CLI tool to install and
configure them.
What is NPM?
Node Package Manager (NPM) is a tool that runs on the NodeJS
runtime environment. It comes with the installation of Node.js. Its purpose is
to download and install packages for a particular project. Those packages and
respective versions are tracked on a JSON file on the root folder of the
project. With NPM, we can also install other CLI tools that can be specific for
scaffolding startup codebase for a particular JavaScript framework. Some
examples include, but not limited to, are: yeoman, create-react-app, angular
CLI.
NPM has many commands, but the install command is the most
basic and most important one, as this is the one that enables us to install and
update packages. Let’s look at some of these commands:
Command |
Description |
$ npm install
package-name –save |
Installs a package latest
version and saves the reference the package.json file |
$ npm install
package-name |
Installs a package but does not
store any reference information |
$ npm update
package-name |
Updates a package with a new
release. NPM decides what version to select |
$ npm install package-name@latest |
To have better control on what
version to install, we can provide the version number or latest release flag
right after the package name, separated by the @ character |
$ npm install -h |
Shows help information on
running the install command |
$ npm run script-name |
Runs a script command defined on
the package.json for build, test, starting the project |
$ npm install -g npm@next |
This command is used to install
the next version of NPM. The -g flag should be used to install this globally
in the system |
What is package JSON?
Package.json is a metadata file which host all the project
related information like project name, licensing, authors, hosting, location
and most importantly information to track project dependencies and scripts to
run.
When installing NPM packages to a project, the information
is saved on a file at the root of the project, package.json. This file
maintains project information and all the package dependencies. By tracking the package dependencies, a
development environment can be easily created it. The developers only need to
clone the repo or codebase and use NPM to download all the dependencies by
typing the following command from the root folder of the project:
$ npm install |
*Note:
package.json must exist in the same folder location where this command
is typed
The script area of the package.json file provide commands
that can be used to create production quality builds, test plan execution, coding
standard validation and running the application. These are essential command
for the day-to-day development operations and integration with CICD tools like
GitHub Actions or Jenkins.
Keep Node.js Updated
With a better understanding of Node.js and the purpose of
NPM for the development process, we can now discuss how to deal with
situations when NPM fails to install a package because our Node.js installation
is behind a few versions, and we need to upgrade it.
What is NVM?
The best way to update Node.js is by using another CLI tool,
Node Version Manager (NVM). This tool enables us to manage multiple versions of
Node.js in our development workspace. It is not a required tool, but it is
useful because it enables us to upgrade and test the application to the latest
releases, which can help us identify compatibility issues with the new runtime
or NPM packages. It also enables us to downgrade to previous version to help us
verify when a feature started to break.
To install NVM on Linux, we can run the following command:
$ curl -o-
https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash |
Once the tool is installed, we can run a few commands to
check the current Node.js version, install a new one and switch between
versions. Let us review those commands:
Command |
Description |
$ nvm version |
Shows the selected node version |
$ nvm –version |
Shows the nvm cli version |
$ nvm ls |
Lists all the Node.js versions
installed |
$ nvm use
version-number |
Selects a Node.js version to use |
$ nvm install
version-number |
Installs a Node.js version |
To install a new version of
Node.js, we can use the install command. This downloads and install a new
version. After the version is installed, the environment should default to the
new version of Node. If the environment was encountering the unsupported
Node.js version error, we can run the NPM command that failed again, and since
the new version is installed, it should be able to install the new package.
Conclusion
When working with JavaScript frameworks like React or
Angular, the Node.js runtime environment must be installed and kept with the
latest version. When new NPM packages
need to be installed on our projects, we need to make sure that they are
compatible with the current version of the runtime environment, Node.js. If this is not the case, the NPM package fails to install, and we need to update
the runtime with the next or latest version. Using tools like NPM and NVM, we
can manage the different versions of packages and runtime respectively. Understanding the purpose of these tools and
how to use them is part of the web development process, and it should help us keep
the development environment up to date.
0 comments :
Post a Comment
What do you think?