Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I run a node script through Grunt?

I am looking to run a node command through my gruntfile. I just need to run:

node index.js

as the first task before any other tasks. I tried searching around but haven't found the answer. I believe it might be something simple but I am not sure how. Do I need to load in nmp tasks?

This is how my Gruntfile looks like:

"use strict";

module.exports = function(grunt) {

  // Project configuration.
  grunt.initConfig({
    jshint: {
      all: [
        'Gruntfile.js'
      ],
      options: {
        jshintrc: '.jshintrc',
      },
    },

    // Before generating any new files, remove any previously-created files.
    clean: {
      tests: ['dist/*']
    },

    // Configuration to be run (and then tested).
    mustache_render: {
      json_data: {
        files: [
          {
            data: 'jsons/offer.json',
            template: 'offers.mustache',
            dest: 'dist/offers.html'
          },
          {
            data: 'jsons/profile.json',
            template: 'profile.mustache',
            dest: 'dist/profile.html'
          }
        ]
      }
    }
  });


  // These plugins provide necessary tasks.
  grunt.loadNpmTasks('grunt-contrib-watch');
  grunt.loadNpmTasks('grunt-contrib-jshint');
  grunt.loadNpmTasks('grunt-contrib-clean');
  grunt.loadNpmTasks('grunt-mustache-render');

  // Whenever the "test" task is run, first clean the "tmp" dir, then run this
  // plugin's task(s), then test the result.
  grunt.registerTask('default', ['clean', 'jshint', 'mustache_render']);


};

I want to run "node index.js" before the 'clean' task.

like image 785
Blueboye Avatar asked Jun 18 '26 08:06

Blueboye


1 Answers

The standard way of doing this is to use the grunt-run task.

Do:

npm install grunt-run --save-dev

And in your grunt file:

grunt.loadNpmTasks('grunt-run');

And then some config (from the documentation):

grunt.initConfig({
  run: {
    options: {
      // Task-specific options go here.
    },
    your_target: {
      cmd: 'node',
      args: [
        'index.js'
      ]
    }
  }
})

Then you change the task registration to be this:

grunt.registerTask('default', ['run', 'clean', 'jshint', 'mustache_render']);
like image 55
Andrew Eisenberg Avatar answered Jun 21 '26 02:06

Andrew Eisenberg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!