npm: how to run test & lint on each change?

18,175

So you should have a definition of the start in your package.json to first run lint, then test then the actual run server. You can find an example in following post: http://substack.net/task_automation_with_npm_run

you should run the 'npm run monitor' command to start the monitoring and the restart should call the npm run start script.

but basically you want to have (based on your package.json)

"scripts": {
    "start": "npm run lint & npm run test & node ./myfile.js",
    "lint": "jshint **/*.js",
    "test": "mocha --recursive",
    "monitor": "nodemon ./bin/www"
    .....
Share:
18,175

Related videos on Youtube

MarcoS
Author by

MarcoS

I'm a sailor. I love roaming in the Mediterranean sea on any wood piece you can stick a mast and a sail on. In my spare time I do software engineering in Turin, as a full-stack developer. I start my coding ages ago with x86 assembly; then I go with C, bash, Perl, Java; Android; currently I do MEAN stack: MongoDB, Express.js, Angular.js and of course Node.js. Obviously on the shoulders of the GNU/Linux O.S..

Updated on September 14, 2022

Comments

  • MarcoS
    MarcoS over 1 year

    I am using a bare npm ( no grunt/gulp) approach to develop my new MEAN project.
    My config is like the following:

    File package.json:

    ...
    "start": "nodemon ./bin/www",
    "lint": "jshint **/*.js",
    "test": "mocha --recursive",
    "dependencies": {
      ...
    },
    "devDependencies": {
      ...
    },
    

    Before starting, I run an npm start and nodemon starts monitoring my project tree for changes, triggering a restart after each source file change.
    So far so good.

    But what if I'd like to include - say - the lint and/or the test stages on each restart?
    I didn't find any clue nor in the nodemon page nor in the npm one...

  • MarcoS
    MarcoS over 8 years
    What is ./myfile.js ?
  • sagie
    sagie over 8 years
    your app.js file that you start your application with.
  • MarcoS
    MarcoS over 8 years
    I don't understand what you mean, sorry: if I call npm run monitor, how npm start will be called?
  • sagie
    sagie over 8 years
    If thats how you startup your app than yes.
  • sagie
    sagie over 8 years
    See github.com/remy/nodemon#running-non-node-scripts so you can have nodemon --exec "npm run start"
  • MarcoS
    MarcoS over 8 years
    Hmmm... I suppose I'll drop "monitor" and will use "start": "npm run lint & npm run test & nodemon ./bin/www" ... :-)
  • sagie
    sagie over 8 years
    that might end up as an endless loop as you will run nodemon from nodemon. so just make sure that doesn't happen.
  • MarcoS
    MarcoS over 8 years
  • sagie
    sagie almost 6 years
    @Sobiaholic meaning? error? what? for me it works great even 3 years after I answered it.