Urgent.News

What's breaking now, across thousands of outlets.

Tech

npm Explained: The Package Manager Behind JavaScript

Introduction Almost no JavaScript project is written from scratch: you build on libraries — React, Express, lodash — written by other people. npm (Node Package Manager) is how those libraries reach your project: it is both the world's largest software registry (millions of packages) and the command-line tool that installs and manages them. If you use Node.js (the Node.js post), you already have…

JavaScript projects rarely start from scratch; they instead leverage libraries — such as React, Express, or lodash — created by other developers. npm (Node Package Manager) serves as the mechanism through which these libraries are made available to your project. It is simultaneously the largest software registry in the world, hosting millions of packages, and the command-line tool used to install and manage them. If you are working with Node.js, you already possess npm.

To install a package, you execute a single command: `npm install react`. This action downloads the React library into the `node_modules/` directory and records it in the `package.json` manifest, which every JavaScript project maintains. The `package.json` file includes a `dependencies` object, for example:

```json

{

"dependencies": {

"react": "^18.2.0"

}

}

```

The caret symbol (`^`) signifies that updates within the same major version (in this case, 18.x) are permissible, but updates to the next major version (19.0 or higher) are not.

Tools required for development, such as test runners or linters like ESLint, are typically stored under the `devDependencies` section instead. This arrangement enables production installations to exclude these development tools. Additionally, the `node_modules/` directory itself is never committed to a Git repository due to its often extensive size — potentially containing tens of thousands of files.

Consequently, upon initiating a fresh clone of a repository, the `npm install` command is executed to rebuild this directory.

The `scripts` section within `package.json` enables the definition of named commands, thereby standardizing how a project operates across different machines. For instance:

```json

{

"scripts": {

"start": "node server.js",

"test": "jest",

"build": "webpack"

}

}

```

Executing `npm test` will perform the same task consistently across all environments. Another crucial file is `package-lock.json`, which meticulously logs the exact versions of all installed packages and their transitive dependencies (dependencies of dependencies). In the absence of this file, two developers installing the same package on different days might end up with diverse versions and potentially varying bugs.

However, with `package-lock.json` in place, installations become reproducible, fostering consistency across development setups.

Committing the `package-lock.json` file is imperative, as it ensures that installations remain identical across various machines, transforming the notion of "works on my machine" into "works everywhere."

The enormity of npm's scale also presents inherent risks, notably the potential for supply-chain attacks where a compromised package could be introduced. Consequently, teams must diligently audit dependencies and maintain locked versions. While alternatives like Yarn and pnpm offer enhanced speed and disk usage, npm remains the prevailing default, and its core concepts — including the manifest, lockfile, scripts, and registry — are universally adopted by package managers across various programming languages.

In summary, npm facilitates the installation of packages into the `node_modules/` directory, documenting them in `package.json`, standardizing commands through scripts, and guaranteeing reproducibility via the lockfile. This infrastructure underpins the modular nature of modern JavaScript, enabling extensive code reuse, while the lockfile serves as a critical safeguard against discrepancies across development environments.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

Local clipboard history without cloud sync — how ClipStack works on Edge

Local clipboard history without cloud sync — how ClipStack works on Edge If you jump between forms, tickets, and chat all day, you already know the pain: you copy an address, then an invoice ID, then…

  • ClipStack extension offers local clipboard history on Edge without cloud sync.
  • Stores history in chrome.storage or IndexedDB for privacy and security.
  • Provides features like snippets, Watch mode, and PIN lock for enhanced productivity.

How to move a PowerPoint deck into a brand template without retyping it

Someone hands you a finished deck. Marketing hands you the new company template. You're told to "just put it in the template". An hour later you're retyping titles into placeholders.

  • Template consists of slide master, layouts, and theme
  • Transfer deck by reusing slides, copy/paste with theme, or using SlideFit tool
  • Verify conversion and manually adjust skipped elements

Finding UK importers of a product by HS code with HMRC's open data

Not affiliated with HMRC. The data comes from HMRC UK Trade Info, published under the Open Government Licence v3.0. If you sell something, source something or research a market, a common question is…

  • UK importers identified by HS code via HMRC open data
  • Guide explains HS and CN8 code conversion, searching by code, flow, and month
  • Python snippet provided for exporting and reading dataset

More from Tuesday 27 October →