How can I reference package version in npm script?

Evaldo Bratti :

I want to reference my package version in a npm script so I can show current version in the app. Something like

{
  "name": "bla",
  "version": "1.0.0",
  "author": "bla bla",
  "scripts": {
    "build": "node VERSION=<<package.version>> build/build.js"
  }
}

Is there a way to do this?

RobC :

1) Referencing package version in npm-scripts.

In npm-script's you can reference the version using the variable npm_package_version. For example:

  • Using a bash shell (E.g. Linux, macOS):

    {
      ...
      "version": "1.0.0",
      "scripts": {
        "build": "echo $npm_package_version"
      }
    }
    

    Note the $ prefix

  • Using Windows (E.g. cmd.exe, Powershell):

    {
      ...
      "version": "1.0.0",
      "scripts": {
        "build": "echo %npm_package_version%"
      }
    }
    

    Note the % prefix and suffix

  • Cross platform

    To utilize one syntax cross-platform check out the package cross-var


2) Referencing package version in node script.

The package version can also be referenced in your a app/node script (i.e. build.js) as follows:

const VERSION = process.env.npm_package_version;
console.log(VERSION); // --> 1.0.0

3) Replacing a placeholder string in a .js file with package version.

Another way to achieve this is to specify a placeholder text string within your JavaScript file. Lets say we have a file named build.js and within that file we have a variable named VERSION declared as follows:

// build.js
const VERSION = '@VERSION@'

As you can see, the placeholder text string is @VERSION@.

You can then install and utilize the package called replace in an npm-script as follows:

{
  ...
  "version": "1.0.0",
  "scripts": {
    "add-version":  "replace -s \"@VERSION@\" $npm_package_version build/build.js"
  }
}

Running npm run add-version will replace the instance of @VERSION@ with the package version (i.e. 1.0.0), in the file named build.js. This solution will hard-code the npm package version into the resultant file.

Note: The to string in the add-version script (above) currently uses the $ prefix (i.e. $npm_package_version) to access the variable, so this will only run successfully on a bash shell. However, for cross-platform usage you'll need to use cross-var as explained in section one (above). In which case the add-version script can be defined as follows:

{
  ...
  "version": "1.0.0",
  "scripts": {
    "add-version":  "cross-var replace -s \"@VERSION@\" $npm_package_version build/build.js"
  }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to fetch package version and date in npm script?

How can I install npm package smaller than any specific version

How can I update npm package on github

How can I reference a project as a package dependency with a specific version in AppVeyor and GitVersion?

How I can update nodejs and npm version?

How can I keep a reference version of a computer?

How can I run mocha with npm script?

How Can Knex (or Any NPM Package) Run a Different Version of Node?

How can I get the version argument of a find_package() call in my find module script?

How do I install the latest minor version of a package on npm?

How do I reference the package version in chocolateyinstall.ps1?

How can I distribute an NPM package such that React uses the babel-compiled version and React Native uses the uncompiled code?

How can i make a npm package that can be installed in global

React | Npm package - How can I export 2 components for use as an npm package

How can I check if a NPM package name is available?

How can I disable NPM package restore in Visual Studio 2015?

With ExpressJS, how can I utilize an NPM package on the client-side?

How can I install the OracleDB NPM package on a Heroku NodeJS server?

How can I replace the bower package jsTimezoneDetect in npm

How can I import and use npm package in my Astro component?

How can I reference the store path of a Nix package?

How can I get rid of package reference conflicts in a VSIX project

How can I embed / reference .NET references in a NuGet package?

How can I change the version of npm using nvm?

How can I find previous version of NPM @types

How can I set the npm version in gitlab CI?

How can I target a git #branch@version with npm?

why I can't install specific version of npm and how to do it?

How can I fix errors when installing npm version 6.4.1