Gruntfile.js task cannot read package values?
In the initConfig function Gruntfile.js I have the following:
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
banner: '/*! <%= pkg.title || pkg.name %> - v<%= pkg.version %> - ' +
'<%= grunt.template.today("yyyy-mm-dd") %>\n' +
'* Copyright (c) <%= grunt.template.today("yyyy") %> <%= pkg.author.name %>;',
concat: {
options: {
banner: '<%= banner %>',
stripBanners: true
},
dist: {
src: ['src/<%= pkg.name %>.js'],
dest: 'dist/<%= pkg.name %>.js'
}
},
I am creating a pkg variable and then trying to pull the name from the object in concat.dist because it comes from the new grunt-init template. I am getting Can't read property 'name' from undefined when running concat: dist. I checked for file existence and node "name" in package.json file.
Considering I'm new to node, I'm not sure if these closures persist across grunt tasks, and if they do, am I using the wrong convention? Is it possible?
source to share
I am guessing that it cannot read your file for some reason package.json
. Or because it does not exist (differently, perhaps?) Or does not have read rights. You can create a simple test to see if you can access the file from Grunt:
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
logvar: {
data: '<%= pkg.name %>'
}
});
grunt.registerTask('logvar', function() {
grunt.log.writeln(grunt.config.get('logvar').data);
});
Now just run grunt logvar
from the command line. If you still get the error, then we have fixed something else, which means your file is package.json
not available. I would recommend checking that it is in the same directory as Gruntfile.js
and that it has read permissions.
source to share