How Version Control and Build Numbering works in Node.js
Version Control and Build Numbering in Node.js
In Node.js, version control and build numbering are typically managed using the package.json
file and tools like npm
and yarn
. The versioning scheme often follows Semantic Versioning (SemVer), which uses a three-part version number: MAJOR.MINOR.PATCH
.
- MAJOR: Incremented for incompatible API changes.
- MINOR: Incremented for new functionality in a backward-compatible manner.
- PATCH: Incremented for backward-compatible bug fixes.
How Version Control Works
-
Version Number in
package.json
: The version number is specified in thepackage.json
file.{ "name": "my-app", "version": "1.0.0", "scripts": { "patch": "npm version patch", "minor": "npm version minor", "major": "npm version major", "release": "standard-version" } }
-
Version Incrementing: You can increment the version number using npm scripts:
npm version patch
: Increments the patch version (1.0.0 to 1.0.1).npm version minor
: Increments the minor version (1.0.0 to 1.1.0).npm version major
: Increments the major version (1.0.0 to 2.0.0).
-
Commit and Tag: Running these commands will also create a git commit and tag with the new version.
Automating Version Bumps with standard-version
standard-version
is a tool that helps automate the versioning and changelog generation.
-
Install
standard-version
:npm install --save-dev standard-version
-
Configure
package.json
: Add arelease
script:{ "scripts": { "release": "standard-version" } }
-
Run
npm run release
: This command does the following:- Bumps the version in
package.json
. - Creates or updates a changelog.
- Commits the changes to git.
- Creates a git tag for the new version.
- Bumps the version in
-
Example Usage:
npm run release
This command will determine the next version number based on the commits since the last release. It follows the Conventional Commits specification, which standardizes commit messages to determine the version bump.
Manually Setting a Build Number
While the typical process involves automatic version increments, you can manually set a version number if needed.
- Set Version Manually:
This command sets the version innpm version 1.2.3
package.json
to 1.2.3, commits the change, and tags it in git.
Example Workflow
Here’s a step-by-step example of how version control and build numbering might work in a Node.js project using npm
and standard-version
:
-
Initial Setup:
- Create a
package.json
file with the initial version.{ "name": "my-app", "version": "1.0.0", "scripts": { "release": "standard-version" } }
- Create a
-
Install Dependencies:
npm install --save-dev standard-version
-
Make Code Changes:
- Develop new features or bug fixes.
- Commit changes with Conventional Commit messages:
git commit -m "feat: add new user registration feature" git commit -m "fix: correct validation error in login"
-
Release a New Version:
- Run the release script:
npm run release
- This will:
- Determine the next version based on the commit messages.
- Update the
package.json
version. - Update the changelog.
- Commit these changes.
- Tag the new version in git.
- Run the release script:
-
Manual Version Setting (if needed):
- If you need to set a specific version manually:
npm version 2.0.0
- If you need to set a specific version manually: