6

I am trying to create a script in my package.json file that will launch my nodemon app, then trigger a gulp sass watch

Currently, I can do this by running a npm launch which starts nodemon, then in a separate terminal window I can run gulp watch to trigger the sass watch from my gulp file.

I would like to create a single script command in package.json that will do both of these- is that possible?

package.json

  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "nodemon app.js",
    "launch": "nodemon app.js && gulp watch"
  },

gulpfile.js

const { src, dest, watch } = require("gulp");
const sass = require('gulp-sass')(require('node-sass'));

function generateCSS(cb) {
    src('./sass/**/*.scss')
        .pipe(sass().on('error', sass.logError))
        .pipe(dest('public/css'));
    cb();
}

function watchFiles(cb) {
    watch('sass/**/**.scss', generateCSS);
}

exports.css = generateCSS;
exports.watch = watchFiles;

edit: please see my answer below for what worked for me, inspired by the answer from @cmgchess

3
  • 1
    something like this? stackoverflow.com/questions/39172536/… Commented Aug 7, 2022 at 17:52
  • no snap, that worked! according to that SO post '&&' executes the commands sequentially, while '&' executes the commands in parallel. So perhaps because nodemon launch but does not end, it never gets around to executing gulp watch when using &&. @cmgchess if you want to submit this as an answer I'll gladly confirm and upvote Commented Aug 8, 2022 at 0:29
  • 1
    I think since you already found the Answer in the link it would be better if I do not repost. Anyway you are free to answer your own question with what worked for you Commented Aug 8, 2022 at 3:39

3 Answers 3

4

Just use the &&

package.json

"build": "npm run base && npm run utilities:global && npm run utilities:unstyled && npm run utilities:styled && npm run components && npm run merge:unstyled && npm run merge:styled && npm run rtl && npm run prejss && npm run themes && npm run full",
Sign up to request clarification or add additional context in comments.

1 Comment

well I had that thought too and actually line 3 in my scripts object in my original post currently has the && in it, however it is not kicking off the gulp watch, only the nodemon service
2

You should be able to use the npm-run-all package and its run-p command to execute multiple scripts side-by side:

"scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "nodemon app.js",
    "watch": "gulp watch",
    "launch": "run-p start watch"
},

Then you can just do:

npm launch

2 Comments

@JKRishe I think this would certainly work but after reading @cmgchess comment above I wonder if using & is better suited for my use case, since my script is relatively simple. either way I'll keep this package under my cap, thank you- seems like it will be very useful for complex setups!
@kdub1312 I can't imagine there's any real problem with using &. I prefer the npm-run-all approach because it makes things cleaner - the scripts are defined individually and can be run individually, and modified if need be. Then the launch script very succinctly conveys that it's running those scripts in parallel.
2

per @cmgchess answer in the comments, it turns out using a double ampersand runs the command sequentially. however, there is also the option to use a single ampersand, which will run them in parallel. In this case, this is what I needed:

"launch": "nodemon app.js & gulp watch"

edit: ok the first time I ran the launch it worked fine. however, subsequent launches crashed. I have it working again now over multiple launches, and what I had to do additionally is switch the order of the commands:

"launchreverse": "gulp watch & nodemon app.js"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.