Homelab, Linux, JS & ABAP (~˘▾˘)~
 

[nodejs] Managing Packages with NPM

These are my notes while doing the course APIs and Microservices on https://www.freecodecamp.org. I highly recommend it if you prefer to try things directly rather than watching videos.


The Node Package Manager (npm) is a command-line tool to share and control modules (or packages) of JavaScript code written for use with Node.js.

When starting a new project, npm generates a package.json file. This file lists the package dependencies for your project. Since npm packages are regularly updated, the package.json file allows you to set specific version numbers for each dependency. This ensures that updates to a package don’t break your project.

npm saves packages in a folder named node_modules. These packages can be installed in two ways:

  1. globally in a root node_modules folder, accessible by all projects.
  2. locally within a project’s own node_modules folder, accessible only to that project.

The package.json file is the center of any Node.js project or npm package. It stores information about your project, similar to how the <head> section of an HTML document describes the content of a webpage. It consists of a single JSON object where information is stored in key-value pairs.

https://github.com/freeCodeCamp/boilerplate-npm/

{
  "name": "fcc-learn-npm-package-json", // your project name
  "author": "Max Mustermann",
  "description": "A project that does something awesome",
  "keywords": [ "descriptive", "related", "words", "freecodecamp" ],
  "license": "MIT", // inform users of what they are allowed to do with your project
  "version": "1.2.0", // describes the current version of your project
  "dependencies": {
    "package-name": "MAJOR.MINOR.PATCH", // Semantic Versioning
    "express": "^4.14.0", 
    "moment": "~2.10.2" // handy library for working with time and dates.
  }
}

PATCHes are bug fixes and MINORs add new features but neither of them break what worked before. Finally, MAJORs add changes that won’t work with earlier versions. Find a more detailed explanation here.

To allow an npm dependency to update to the latest PATCH version, you can prefix the dependency’s version with the tilde (~) character.
The caret (^) will allow both MINOR updates and PATCHes.

Leave a Reply

Your email address will not be published. Required fields are marked *