1

I'm using the following in my Gruntfile:

grunt.initConfig({
    assets: grunt.option('assets'),
    config: grunt.file.readJSON(path.join('<%= assets %>', 'config.json')) || grunt.file.readJSON('./defaults.json'),
    ...
})

When I execute it, it throws:

>> Error: Unable to read "<%= assets %>/config.json" file (Error code: ENOENT).
    >> at Object.util.error (/.../prj/node_modules/grunt-legacy-util/index.js:54:39)
    >> at Object.file.read (/.../prj/node_modules/grunt/lib/grunt/file.js:247:22)
    >> at Object.file.readJSON (/.../prj/node_modules/grunt/lib/grunt/file.js:253:18)
    >> at Object.module.exports (/.../prj/Gruntfile.js:10:28)
    >> at loadTask (/.../prj/node_modules/grunt/lib/grunt/task.js:325:10)
    >> at Task.task.init (/.../prj/node_modules/grunt/lib/grunt/task.js:437:5)
    >> at Object.grunt.tasks (/.../prj/node_modules/grunt/lib/grunt.js:120:8)
    >> at Object.module.exports [as cli] (/.../prj/node_modules/grunt/lib/grunt/cli.js:38:9)
    >> at Object.<anonymous> (/usr/local/lib/node_modules/grunt-cli/bin/grunt:45:20)
    >> at Module._compile (module.js:425:26)

Wondering if this is because the assets var isn't defined at the point I'm trying to use it? Or is the <%= %> syntax not allowed to be used in this fashion?

According to this answer, it looks like it should work -- which I found because previously I was just using var assets = grunt.option('assets'), but that was throwing SyntaxError: Unexpected token var for some reason. (Before I messed with it, it looked like this:)

module.exports = function(grunt) {
    require('load-grunt-tasks')(grunt)

    var util = require('util'),
        path = require('path'),
        pkg = require('./package.json')

    var assets = grunt.option('assets'),
    var config = grunt.file.readJSON(path.join(assets, 'config.json')) || grunt.file.readJSON('./defaults.json')

    grunt.initConfig({
        ...
    })

What's the proper, grunt way of using a module or declaring a variable inside a gruntfile like this? And/or, can I fix the problem with Unexpected token var?

(Note: It's not the config file that I'm having trouble loading, it's the fact that the assets path from grunt.option() appears to not be interpreted)

Community
  • 1
  • 1
brandonscript
  • 68,675
  • 32
  • 163
  • 220

3 Answers3

1

Seems like you want to read a custom json file. Remember that even grunt script is just JavaScript and node requires work just fine. So you can do something like

 var conf = grunt.file.exists(assets + '/ '+ 'config.json') ? require(assets + '/ '+ 'config.json') : {};

You can then use your config variable around where ever.

conf.foo || 'default'
conf.bar

In either case you need to declare assets variable before usage. In require or in initConfig

Update

Also,
You have a extra comma after var assets = grunt.option('assets'), Either remove that or remove the var in next line

Shaunak
  • 17,377
  • 5
  • 53
  • 84
  • The config file loads fine; it's `grunt.option('assets')` that it's breaking on I think – brandonscript Aug 08 '16 at 21:13
  • where do you define `assets` then? is it a variable or a parameter? – Shaunak Aug 08 '16 at 21:14
  • Originally I had it as `var assets = grunt.option('assets')`. Then it started gawking at me about using `var` inside the gruntfile, so I switched to trying to use `assets: grunt.option('assets')` to load the arg, so that `assets` becomes a grunt variable. – brandonscript Aug 08 '16 at 21:16
  • I see. Okay try without using path.join(). Try joining the string yourself using asset to see if it works, something like `config: grunt.file.readJSON('<%= assets %>/'config.json')` – Shaunak Aug 08 '16 at 21:20
  • Hrm, nope even that doesn't work. Probably can't use it at all like this -- like what @ahmadbamieh's answer states. – brandonscript Aug 08 '16 at 21:23
  • right.. so you can only use it in simple key value pairs. In my grunt files, I just load the configs like I showed using a regular require. It should be a valid json file though. – Shaunak Aug 08 '16 at 21:24
  • See my edit to the question - originally I was using `var assets = grunt.option('assets')` and it was working just fine. Now it's not though - throwing the `Invalid token var` error. – brandonscript Aug 08 '16 at 21:27
  • You have a extra comma after `var assets = grunt.option('assets'),`. Either remove that or remove the `var` in next line – Shaunak Aug 08 '16 at 21:30
  • FACEPALM. Nice catch. Unbelievable. Edit your answer with that at the top, I'll accept. – brandonscript Aug 08 '16 at 21:31
  • ha ha.. Happens to all of us :) – Shaunak Aug 08 '16 at 21:32
0

You cannot declare var assets = .. inside the grunt.init( { Curly braces, as that is not valid JS syntax. What you need to do in order for it to understand the <%= %> syntax is declare it like so:

grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    uglify: {
        options: {
            banner: '/*! <%= pkg.name %>'
            // pkg is a property on the json object passed into initConfig
...
J-Dizzle
  • 3,176
  • 6
  • 31
  • 49
  • if you declare the assets variable before the initCongfig, you could do `pkg: assets,`. Not sure what your doing with _grunt.option()_ – J-Dizzle Aug 08 '16 at 21:10
  • Not sure how that's different from what I'm doing exactly? Other than that your example reads a json file, and mine is loading a command line arg? – brandonscript Aug 08 '16 at 21:11
0

The <%= %> is an interpolation string specified by grunt, if you use it in non-grunt parameters, it will not be interpolated (such as your case in using it in path.join).

to make it work you have to use

grunt.template.process('<%= asset %>', {
   data: {
      assets: grunt.option('assets')
   }
})
Bamieh
  • 10,358
  • 4
  • 31
  • 52
  • Alright, that explains why the interpolation syntax isn't working. Any idea why my original way using `var` would no longer be working? – brandonscript Aug 08 '16 at 21:20
  • yes, the interpolation takes from 'this' which is the object itself, hence in order to get the value of var, you have to put it in the object of `initConfig` such as `initConfig({assets: assets, //...}` – Bamieh Aug 08 '16 at 21:22