2

I have a package.json file and I have this script to create 2 separate files one for version and other one for date.

When i run the command, it generates the version-npm.txt with correct data holding the current version, but the version.txt file is generated with the exact script instead, it contains date +"%d/%m/%Y %T"

"scripts": {
    "versions": "node -e \"console.log(require('./package.json').version);\" > ./public/version-npm.txt && echo `date +\"%d/%m/%Y %T\"` > ./public/version.txt" 
}

I need this to be updated with the current date and time. is there any other way to it or can you help me fix it?

i tried writing the date as \"$(date)\" still the same, now i get \"$(date)\"

Amit Pandey
  • 274
  • 4
  • 16

1 Answers1

2

Why is it not working?

  • The way that you are obtaining the date, i.e. date +\"%d/%m/%Y %T\" or similarly using \"$(date)\" does work successfully on *nix platforms, whereby npm utilizes sh by default to run npm scripts.

  • However, the aforementioned method does not work on Windows because npm on that OS utilizes cmd as the default shell to run npm scripts - cmd simply does not understand the date command.


Solution

The following solution will work cross-platforms (Windows, Linux, MacOS etc) :

  1. Firstly cd to your project directory and install the moment package by running the following command:

    npm i -D moment
    

    We'll utilize this package to obtain the date/time formatted as DD/MM/YYY HH:MM:SS.

  2. Then redefine your versions script in the scripts section of your package.json as follows:

    "scripts": {
      "versions": "node -p \"process.env.npm_package_version\" > ./public/version-npm.txt && node -p \"require('moment')().format('MM/DD/YYYY HH:mm:ss')\" > ./public/version.txt"
    }
    

Explanation:

  1. The npm script (above) utilizes the nodejs command line option -p to evaluate and print the result of the following inline JavaScript:

    process.env.npm_package_version
    

    This essentially utilizes nodejs process.env to read the environment variable npm_package_version which npm creates. See my answer here for further explanation.

    The version is then redirected (>) to the file using the same method as per your attempt:

    > ./public/version-npm.txt
    

    Note: You could continue to utilize your current, more verbose, solution to obtain the version from package.json if you prefer, i.e.

    node -e \"console.log(require('./package.json').version);\"
    

  2. Next we obtain the date. Again we utilize the nodejs command line option -p to evaluate and print the result of the following inline JavaScript:

    require('moment')().format('MM/DD/YYYY HH:mm:ss')
    

    The date value is redirected (>) to the file as follows:

    > ./public/version.txt
    
Community
  • 1
  • 1
RobC
  • 22,977
  • 20
  • 73
  • 80
  • 1
    **Note:** If your are using an early version of node.js then you may encounter errors with `moment`. In which case install an earlier version. For example run `npm i -D moment@1.7.2` instead of `npm i -D moment` – RobC Jun 03 '20 at 15:26
  • Thanks a lot, it works and you explained it very well :) – Amit Pandey Jun 03 '20 at 15:34