March 5, 2018
Throwing Errors in Npm Build Script
From time to time you may want to throw an error from within an npm build script. This should be done in such a way that the complete execution of the script is recognized as faulty. So that for example, a build server recognizes that a script did not run successfully. You only need to output the correct exit code in order to achieve this behaviour. Ideally, the user should also receive an error message that is as accurate as possible.
Lets take a look at the code:
"build": "echo \"Error: Don't call this Build-Script - its deprecated. Use 'npm run build:dev' instead \" && exit 1"
All we do is to echo some text and then we append ‘exit 1’ which indicates unsuccessful termination. Thats it.