Npm packages

Npm install

Packages are folders of reusable code published with a name and a version number. Your project records each choice in package.json so every machine installs the same set. The command is npm install, and npm writes the chosen version into that record.

Installing adds the files to a local folder and updates that record for you. From then on the dependency is part of the project, not a copy pasted snippet hidden in one file.

Import them

Installed code stays dormant until you import it by name. Named imports make the source visible at the top of the file, so readers see exactly which helper comes from which package.

Keep imports at the top and use clear local names. Short modules with explicit imports are easier to move, test, and review than long files with hidden globals.

import { greet } from "greetlib"; const message: string = greet("Ada"); console.log(message);

The package greetlib is the one the course builds in the publishing lesson, so it is not installed in this folder and the block is not run here. Change the import to a built-in module and the same shape runs: the program below imports a function from node:path and prints its result.

import { basename } from "node:path"; const message: string = basename("/tmp/notes.txt"); console.log(message);

notes.txt

@types concept

Many packages ship their own type declarations, so completion and checking work right after install. Older JavaScript libraries have no bundled types, which is where separate type packages help.

A type package contains only declarations for the matching library. It does not change runtime behavior, it only teaches the checker the shapes of functions, options, and return values.

Pin versions

Versions move forward, and new releases can change behavior. Pinning records an exact version so builds stay identical today, tomorrow, and on a teammate machine.

Update versions on purpose, one package at a time, then run checks again. Small deliberate upgrades are calmer than surprise changes during an unrelated task.

{ "name": "my-app", "version": "1.0.0", "dependencies": { "greetlib": "1.4.2" } }

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What do packages give a project?

    Answer

    Reusable code published with a name and a version

    A package is code somebody else maintains, and the project records which version it uses.

  2. What does npm install do to package.json?

    Answer

    It records the chosen version in the dependencies

    The install writes the dependency and its version range into the project record, so another machine installs the same set.

  3. Why does a named import keep a file readable?

    Answer

    It shows which helper comes from which package

    The import line states the source of every name, which is what a review and a move both need.

  4. What does an @types package contain?

    Answer

    Only type declarations for a library that ships none

    It teaches the checker the shapes of the functions and changes nothing at run time.

  5. Why pin an exact version in package.json?

    Answer

    So every machine builds with the same code

    A range lets a new release in; an exact version keeps today and tomorrow the same until the project moves on purpose.

  6. What does this program print? import { basename } from "node:path"; const message: string = basename("/tmp/notes.txt"); console.log(message);

    Answer

    notes.txt

    basename gives the last part of the path, and the declared string type keeps the result usable.

  7. What does this program print? const path = "/home/ada/notes.txt"; console.log(path.slice(path.lastIndexOf("/") + 1));

    Answer

    notes.txt

    The slice starts after the last slash, which is the file name.

  8. A JavaScript library is installed but ships no declarations. What does a strict project report for import { greet } from "greetlib";?

    Answer

    error TS7016: Could not find a declaration file for module 'greetlib'

    Strict mode refuses an implicitly any module, which is the error a type package removes.

  9. What does the compiler say about this program? import { readFileSync } from "node:fs"; const text: number = readFileSync("notes.txt", "utf8"); console.log(text);

    Answer

    error TS2322: Type 'string' is not assignable to type 'number'.

    The built-in function gives a string, and the annotation refuses it as a number.

  10. What is the difference between a dependency and a devDependency?

    Answer

    A devDependency is needed to build and test, a dependency is needed at run time

    The split keeps compile and test tools out of the production install.

Article author: Arthur Isaev