Last active
December 31, 2020 01:42
-
-
Save djrosenbaum/a56b933914dabd5a941dfcfca900475f to your computer and use it in GitHub Desktop.
Enforce .npmrc on npm install
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Isn't it nice when devs use the same version of node when building on a project | |
I was searching for a way to enforce .nvmrc before running npm install | |
There was an npm package | |
-node-version, but do we really want another dependency in our projects | |
Add the following line inside of package.json | |
"preinstall": "node ./enforceNodeVersion" | |
*/ | |
// access node file system | |
import fs from 'fs'; | |
/** | |
* Checks the version of node in .nvmrc against node's process.version | |
* Exits if the versions do not match | |
* | |
* @returns {undefined} - undefined | |
*/ | |
const enforceNodeVersion = () => { | |
const nvmrc = fs.readFileSync('.nvmrc', 'utf8'); | |
if (nvmrc.trim() !== process.version) { | |
console.log( | |
`Expected Node Version ${nvmrc} but instead found ${process.version} \n Try typing "nvm use" to switch node versions \n` | |
); | |
process.exit(); | |
} | |
}; | |
enforceNodeVersion(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "myApp", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1", | |
"preinstall": "node ./enforceNodeVersion" | |
}, | |
"keywords": [], | |
"author": "", | |
"license": "ISC" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment