Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

package.json: how to test if node_modules exists

Inside package.json, is it possible to test if node_modules directory exists? My goal is to print a message if node_module does not exists, something like:

node_module not existent: use npm run dist

where dist is a script inside scripts of my package.json. Thank you.

like image 725
caneta Avatar asked Apr 21 '26 16:04

caneta


1 Answers

I thought I would post what I've done for cross-platform one-liner conditional NPM scripts.

"scripts": {
    "start":"(node -e \"if (! require('fs').existsSync('./node_modules'))
{process.exit(1)} \" || echo 
'node_module dir missing: use npm run dist') && node start-app.js",
}
like image 121
joshhemphill Avatar answered Apr 24 '26 07:04

joshhemphill



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!