NPM (Node Package Manager) is a package manager for the JavaScript programming language. It is primarily used for managing and installing packages or libraries that are required for building JavaScript applications. NPM comes bundled with Node.js, which is a runtime environment for executing JavaScript code outside of a web browser.
NPM allows developers to easily install, update, and remove packages from their projects. It provides a command-line interface (CLI) that allows developers to interact with the package manager and perform various operations such as installing packages, managing dependencies, and running scripts defined in the package.json file.
To install NPM, you need to first install Node.js on your system. Node.js includes NPM by default, so once you have Node.js installed, you will have access to the NPM command-line tool.
Here is an example of how to install a package using NPM in a JavaScript code snippet:
<!DOCTYPE html> <html> <head> <title>NPM Example</title> </head> <body> <script src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js"></script> <script> // This code snippet demonstrates how to use NPM to install and use the jQuery library // First, include the jQuery library using a script tag // The src attribute points to the jQuery package hosted on a CDN (Content Delivery Network) // This will download and include the jQuery library in your HTML file // Now, you can use jQuery functions in your JavaScript code $(document).ready(function() { // jQuery code here $('body').css('background-color', 'lightblue'); }); </script> </body> </html>
In the above example, we include the jQuery library using a script tag with the src attribute pointing to the jQuery package hosted on a CDN. This will download and include the jQuery library in our HTML file. We can then use jQuery functions in our JavaScript code to manipulate the DOM (Document Object Model) and perform various operations.
Note: The above example assumes that you have an internet connection to download the jQuery library from the CDN. If you want to use NPM to install packages locally, you would typically use a build tool like webpack or parcel to bundle your JavaScript code and dependencies together.