Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gruntfile.js task can't read package values?

In Gruntfile.js' initConfig function, 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'm creating the pkg variable and then attempting to pull the name from the object under concat.dist, because this is coming from a new grunt-init template. I'm getting Cannot read property 'name' of undefined when running concat:dist. I've verified the existence of the file and the node "name" in the package.json" file.

Given I'm new to node, I'm not sure if these closures persist when calling grunt tasks and if they do, am I using the wrong convention? Is this even possible?

like image 910
Shane Avatar asked Dec 30 '14 15:12

Shane


1 Answers

My guess is that it can't read your package.json file for some reason. Either because it doesn't exist (named differently perhaps?) or doesn't have read permissions. 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, well then we've eliminated anything else, and that means your package.json file is inaccessible. I would recommend checking that it is in the same directory as Gruntfile.js and that it has read permissions.

like image 68
Jordan Kasper Avatar answered Nov 15 '22 07:11

Jordan Kasper