Third-party packages and modules are prewritten code packets used to reduce development time by bootstrapping the features readily available in the package or module to write code faster, boost developer experience, or ship code for the users. There are different packages and modules and each one solves a different problem. It could be as simple as an animation package, a unit testing framework, or a back-end framework that allows us to write code that runs on the server side.
Npm (Node Package Manager) is a package manager for the JavaScript programming language. It consists of two parts. The live registry where all packages, both public and private, are stored and displayed. Here, we get to see different packages available for use and the documentation for each package. We also have the Command Line Interface (CLI) that allows us to interact with the available packages.
Npm comes bundled with the Node installer which can be found on the official website. We get access to the features provided by npm once the Node.js installation is complete.
Let's take a look at the steps required to install an npm module.
To initialize a new project using npm, run the npm init
command in a terminal window. This starts a prompt that requires us to either click "Enter" to accept the default value or enter a replacement value for it.
The default entries are in brackets and will only be replaced with our entry if we type out the value that the field should have.
Some fields are empty by default and will only be populated with the value we put in. If none is supplied, the field remains empty, or a predefined string is used as a placeholder.
Completing the prompt creates a package.json
file in our project folder. This file stores project metadata and information on functional project dependencies. This file can be edited later to suit the project's needs. The following is the content of our package.json
before installing a package.
{"name": "using-3rd-party-packages-in-npm","version": "1.0.0","description": "Walk through using third party packages in npm","main": "app.js","scripts": {"test": "echo \"Error: no test specified\" && exit 1"},"author": "","license": "ISC"}
To install an npm package, we run the npm install <package-name>
or npm i <package-name>
command. Running npm i chalk
installs the package used for styling terminal strings. This command installs the latest version of chalk
. We can install an exact version by specifying it using npm i chalk@<version-number>
.
{"name": "using-3rd-party-packages-in-npm","version": "1.0.0","description": "Walk through using third party packages in npm","main": "app.js","scripts": {"test": "echo \"Error: no test specified\" && exit 1"},"author": "","license": "ISC","dependencies": {"chalk": "^5.2.0"}}
Execution of this command pulls the package files from the npm registry, creates, and populates the node_modules
folder. All installed packages can be found in this folder.
The package-lock.json
file is used to store the exact information of the installed packages and keep track of the dependency trees. This ensures that when our project is being replicated on another machine, the exact dependencies are installed.
Click the "Run" button to execute the below code.
import chalk from 'chalk'; console.log(chalk.blue("Hello world"));
If we encounter the ERR_MODULE_NOT_FOUND
error, it is likely because the required chalk
package was not installed. We can resolve this issue by running the following commands in the widget terminal above and see if it works or not.
# After failed initial run,# Run the following command in the terminal:npm install chalk# Run the code using the following command:node app.mjs# See Hello World in blue
Here are a few more commands that we can run with npm:
npm help npm
: Loads a preinstalled webpage with documentation on how to get started with npm.
npm uninstall <package-name>
: Removes a package that was previously installed.
npm run <script-name>
: Executes a custom script.
Free Resources