'Using node-sass watch option with npm run-script
So I'm running tasks in npm package scripts but I want to pass the watch option in npm start.
This works:
"scripts": {
"scss": "node-sass src/style.scss dist/style.css -w"
}
This doesn't compile, watch, or throw any error:
"scripts": {
"scss": "node-sass src/style.scss dist/style.css",
"start": "parallelshell \"npm run scss -- -w\""
}
Doesn't work without parallelshell either or without shorthand.
I assume the problem is the run-script is passing the extra argument in quotes, so the command comes out like:
node-sass src/style.scss dist/style.css "-w"
I'd like this to work without adding any dependencies. What am I missing?
Btw, I'm in Windows 10 with command prompt/git bash.
Solution 1:[1]
Building on the previous answers, another option is to leverage NPM's custom script arguments to remain DRY by not repeating the build script arguments in the watch script:
"scripts": {
"build:sass": "node-sass -r --output-style compressed src/style.scss -o dist",
"watch:sass": "npm run build:sass && npm run build:sass -- -w"
}
In the above example, the watch:sass script works as follows:
- Run the
build:sassscript. This will compile your CSS. - Run the
build:sassscript again, but this time include the-wflag. This will compile your CSS when one of your SASS file changes.
Notice the -- option used at the end of the watch:sass script. This is used to pass custom arguments when executing a script. From the docs:
As of [email protected], you can use custom arguments when executing scripts. The special option -- is used by getopt to delimit the end of the options. npm will pass all the arguments after the -- directly to your script.
Solution 2:[2]
Btw, here's my change:
"scss": "node-sass src/style.scss dist/style.css",
"start": "parallelshell \"npm run scss && npm run scss -- -w\"
Edit: Change was asynchronous script runs, for the initial compile and then with the watch flag.
Solution 3:[3]
Simplest in my opinion, for a smaller quick project, is just open a new bash window and paste:
node-sass src/ -wo dist
Solution 4:[4]
If you want to watch and compile changes automatically when you save that changes in your .scss file , you can use this solution:
"scripts": {
"watch:sass": "node-sass -w path/to/your/scss --output-style compressed",
// example : node-sass -w public/styles/scss/bootstrap.scss public/styles/style.css --output-style compressed
}
Solution 5:[5]
package.json:
"scripts": {
"compile:sass": "node-sass ./styles.scss ./styles.css -w}
npm run compile:sass
note if you run this command the styles does'nt get updated into css file immediately, only if it detects changes in the sass file it updates your css file, not on the initial run.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | |
| Solution 3 | Jujhar Singh |
| Solution 4 | vsync |
| Solution 5 | Battle_Bee |
