How to Use npm Scripts as Your Build Tool”, an egghead.io course
An egghead.io course with lots of useful little tidbits I hadn’t become familiar with yet:
- npm will now load your PATH with your local
node_modulesfolder, so you don’t have to specifically reference local modules anymore via the.binfolder- No more having to do:
"my-script" : "./node_modules/.bin/node-sass input.scss output.css"
- Just reference it normally and it'll look in your local
node_modulesfirst"my-script" : "node-sass input.scss output.css"
- No more having to do:
- You can pass arguments to a script using
--- Compile your CSS
"css" : "node-sass src/styles.scss dist/styles.scss"
- If you wanted to watch your CSS, you might do:
"css:watch" : "node-sass src/styles.scss dist/styles.scss" --watch
- But then you have to sync changes between the two scripts. Instead you could just do:
"css:watch" : "npm run css -- --watch
- Compile your CSS
npm-run-allis a useful little package that’ll let you run all your npm scripts more succinctly- Say you have three scripts you want to run for linting using different tools
"lint:css" : "csslint --option path/to/file""lint:js" : "eslint --option path/to/file""lint:html" : "linter --option path/to/file"
- Instead of doing:
"lint" : "npm run lint:css && npm run lint:js && npm run lint:html"
- You can do fancy things like use globbing:
"lint" : "npm-run-all lint:*
- For more info on all the neat little things it can do, checkout npm-run-all
- Say you have three scripts you want to run for linting using different tools
- Use variables in your
package.jsonby prefixing the variable name with$"version" : "2.1.0"inpackage.jsoncan be accessed in your scripts by doing$npm_package_version- To see variables available to you, do
npm run env - This only works on Mac/linux. You'd have to install something like
cross-varin order to have it work cross-platform.