javascript - <%= %> variable syntax in Gruntfile throws "unable to read" -


i'm using following in gruntfile:

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

when execute it, throws:

>> error: unable read "<%= assets %>/config.json" file (error code: enoent).     >> @ object.util.error (/.../prj/node_modules/grunt-legacy-util/index.js:54:39)     >> @ object.file.read (/.../prj/node_modules/grunt/lib/grunt/file.js:247:22)     >> @ object.file.readjson (/.../prj/node_modules/grunt/lib/grunt/file.js:253:18)     >> @ object.module.exports (/.../prj/gruntfile.js:10:28)     >> @ loadtask (/.../prj/node_modules/grunt/lib/grunt/task.js:325:10)     >> @ task.task.init (/.../prj/node_modules/grunt/lib/grunt/task.js:437:5)     >> @ object.grunt.tasks (/.../prj/node_modules/grunt/lib/grunt.js:120:8)     >> @ object.module.exports [as cli] (/.../prj/node_modules/grunt/lib/grunt/cli.js:38:9)     >> @ object.<anonymous> (/usr/local/lib/node_modules/grunt-cli/bin/grunt:45:20)     >> @ module._compile (module.js:425:26) 

wondering if because assets var isn't defined @ point i'm trying use it? or <%= %> syntax not allowed used in fashion?

according this answer, looks should work -- found because using var assets = grunt.option('assets'), that throwing syntaxerror: unexpected token var reason. (before messed it, looked 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 proper, grunt way of using module or declaring variable inside gruntfile this? and/or, can fix problem unexpected token var?

(note: it's not config file i'm having trouble loading, it's fact assets path grunt.option() appears not interpreted)

seems want read custom json file. remember grunt script javascript , node requires work fine. can like

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

you can use config variable around ever.

conf.foo || 'default' conf.bar 

in either case need declare assets variable before usage. in require or in initconfig

update

also,
have comma after var assets = grunt.option('assets'), either remove or remove var in next line


Comments