Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 12.3 — npm and package.json
JavaScript · Example 1 of 3

How caret, tilde and exact ranges decide

This is a small copy of the rules in the real semver package that npm uses. The real one also handles ranges like >=1.2 <2, 1.x, ||, and pre-release versions such as 2.0.0-beta.1.

npm is the package manager that comes with Node.js: package.json lists your project's name, scripts and the packages it depends on (with version ranges), node_modules holds the installed code, and the lockfile pins the exact versions so every install is identical.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 6 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Write a function bumpType(oldVersion, newVersion) that returns "major", "minor", "patch" or "same".

02

Write the package.json scripts for a project that runs Vite in development, builds with vite build, lints with eslint . and runs the linter automatically before every build.

03

For each change, say which version number you bump: (a) fix a rounding bug; (b) add a new optional currency option; (c) rename the function format to formatPrice.

Explain it without notes

01

What is the difference between dependencies and devDependencies?

02

Explain semver and what ^, ~ and an exact version mean in package.json.

03

Why should you commit package-lock.json, and when do you use npm ci instead of npm install?

04

How do npm scripts find tools like vite and eslint?

How caret, tilde and exact ranges decide

Preview

Press Run to see the page.

Console

Nothing logged yet.

Expected console output

^4.17.21 allows 4.18.0 true
^4.17.21 allows 5.0.0 false
~4.17.21 allows 4.17.30 true
~4.17.21 allows 4.18.0 false
^0.3.1 allows 0.3.9 true
^0.3.1 allows 0.4.0 false
4.17.21 allows 4.17.22 false