Home  Nodejs   How version ...

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.

How Version Control Works

  1. Version Number in package.json: The version number is specified in the package.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"
      }
    }
    
  2. 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).
  3. 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.

  1. Install standard-version:

    npm install --save-dev standard-version
    
  2. Configure package.json: Add a release script:

    {
      "scripts": {
        "release": "standard-version"
      }
    }
    
  3. 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.
  4. 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.

  1. Set Version Manually:
    npm version 1.2.3
    
    This command sets the version in 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:

  1. Initial Setup:

    • Create a package.json file with the initial version.
      {
        "name": "my-app",
        "version": "1.0.0",
        "scripts": {
          "release": "standard-version"
        }
      }
      
  2. Install Dependencies:

    npm install --save-dev standard-version
    
  3. 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"
      
  4. 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.
  5. Manual Version Setting (if needed):

    • If you need to set a specific version manually:
      npm version 2.0.0
      
Published on: Jul 01, 2024, 07:45 AM  
 

Comments

Add your comment