grunt-run icon indicating copy to clipboard operation
grunt-run copied to clipboard

Support for environment variables

Open llonchj opened this issue 8 years ago • 1 comments

Requesting support for environment variables when defining a target as shown below:

grunt.initConfig({
  run: {
    options: {
      // Task-specific options go here. 
    },
    your_target: {
      cmd: 'executable',
     env:{
       'NODE_ENV': 'production'
     },
      args: [
        'arg1',
        'arg2'
      ]
    }
  }
})

llonchj avatar Jul 06 '16 20:07 llonchj

This might be not relevant to the issue creator at this point in time, but I just lost a considerable amount of time through the fact that this feature is so thoroughly documented. So here is what I found:

This feature was initially implemented in #7 and first shipped with v0.3.0.

Take care though:

  1. You need to specify variables as options.env, so differently nested than args, which was quite unexpected for me.
  2. The environment variables specified here are passed directly to child_process.spawn as opts.env. If these are customized, they are overwritten and not merged with the variables of the calling process, what means that e.g. PATH would be missing and e.g. node eventually not found.

So you might need to specify something like:

grunt.initConfig({
  run: {
    my_task: {
      options: {
          env: Object.assign({}, process.env, {
            'NODE_ENV': 'production',
          }),
        },
      },
      args: [
        'arg1',
        'arg2'
      ],
    },
  },
});

mrackwitz avatar Aug 17 '16 15:08 mrackwitz