简体   繁体   中英

npm scripts nodemon - watching js and scss files for changes

I'm experimenting with setting up a dev environment to use NPM only, without the use of grunt.js or bower.js.

I followed this tutorial: http://beletsky.net/2015/04/npm-for-everything.html

I'm using nodemon to watch my .js and .scss files for changes which restarts the node server. So in my package.json file, under scripts I have

scripts:

"watch-js": "nodemon -e js --watch public/js -x \\"npm run build-js\\"",

"watch-sass": "nodemon -e scss --watch public/sass -x \\"npm run build-sass\\"",

"watch": "npm run watch-js & npm run watch-sass"

But when I run npm run watch it only watches for the public/js files to change. And it triggers a build accordingly.

But it won't watch for the sass files.

Versions: node v0.10.36 nodemon v1.4.1

I also include a build script which if I run compiles the sass to css, so my build-sass script should be ok

"build": "npm run build-js & npm run build-sass",
"watch": "npm run watch-js & npm run watch-sass"

If you are using windows, you might be encountering the windows problem

Because npm is reliant on the operating systems shell to run scripts commands, they can quickly become unportable. While Linux, Solaris, BSD and Mac OSX come preinstalled with Bash as the default shell, Windows does not. On Windows, npm will resort to using Windows command prompt for these things

If so, you can try using parallelshell or npm-run-all for better cross platform support.

This works for me in Mac OS

  1. Install nodemon

    npm install --save-dev nodemon

  2. Install npm-run-all:

    npm install --save-dev npm-run-all

  3. In package.json add two scripts:

    "startn": "nodemon ./app.js", "watch-jscss": "gulp watch"

    • Where 'app.js' is your server file
    • "gulp watch" can be replace by whatever task is doing a sass watch
  4. In the console:

    npm-run-all --parallel watch-jscss startn

try to change,

"watch": "npm run watch-js & npm run watch-sass"

to

"watch": "npm run watch-js && npm run watch-sass"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM