javascriptgruntjsgrunt-contrib-watch

Most of Grunt tasks are not found


This is my first day with grunt and I'm trying to make it work using these tutorials

https://24ways.org/2013/grunt-is-not-weird-and-hard/

https://css-tricks.com/autoprefixer/

And my Gruntfile.js is this:

module.exports = function(grunt) {
// 1. All configuration goes here 
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
watch: {
  scripts: {
    files: ['scripts/app.js'],
    tasks: ['uglify'],
    options: {
      spawn: false,
    }//For some reason I had a come here. Don't know if it matters
  },
  css: {
    files: ['content/app.scss'],
    tasks: ['sass'],
    options: {
      spawn: false,
    }
  },
  styles: {
    files: ['content/app.css'],
    tasks: ['autoprefixer']
  }
},
uglify: {
  build: {
    src: "scripts/app.js",
    dest: "scripts/app-final.js"
  }
},
sass: {
  dist: {
    options: {
      style: 'compressed'
    },
    files: {
      'content/app.css': 'content/app.scss'
    }
  }
},
autoprefixer: {
  dist: {
    files: {
      'content/app-prefixed.css': 'content/app.css'
    }
  }
},
imagemin: {
    dynamic: {
        files: [{
            expand: true,
            cwd: 'assets/img/',
            src: ['**/*.{png,jpg,gif}'],
            dest: 'assets/img/'
        }]
    }
}
});


 // 3. Where we tell Grunt we plan to use this plug-in.
 grunt.loadNpmTasks(
'grunt-contrib-uglify',
'grunt-contrib-sass',
'grunt-autoprefixer',
'grunt-contrib-watch',
'grunt-contrib-imagemin'
);

 // 4. Where we tell Grunt what to do when we type "grunt" into the terminal.
grunt.registerTask(
'default', [
  'watch',
  'uglify',
  'sass',
  'autoprefixer',
  'imagemin'
]);
};

But when I try grunt watch watch I get this:

 # grunt watch
 Warning: Task "watch" not found. Use --force to continue.
 Aborted due to warnings.

To make things weirder grunt uglify is seen

# grunt uglify
Running "uglify:build" (uglify) task
>> Destination scripts/app-final.js not written because src files were empty.
>> No files created.
Done, without errors.

Running grunt --help gives me an interesting thing

Available tasks
    uglify  Minify files with UglifyJS. *
    default  Alias for "watch", "uglify", "sass", "autoprefixer", "imagemin" tasks.

I really cannot find a difference between uglify and the other functions. VS Code doesn't give me any errors. I installed all of the used tasks. I have node installed.

Restarting VS Code doesn't help. I don't think this matters but just in case, I'm using Linux.

Reinstalling the dependencies didn't help either


Solution

  • You did the following:

     grunt.loadNpmTasks(
    'grunt-contrib-uglify',
    'grunt-contrib-sass',
    'grunt-autoprefixer',
    'grunt-contrib-watch',
    'grunt-contrib-imagemin'
    );
    

    Replace it with this:

    grunt.loadNpmTasks('grunt-contrib-sass');
    grunt.loadNpmTasks('grunt-contrib-imagemin');
    grunt.loadNpmTasks('grunt-autoprefixer');
    grunt.loadNpmTasks('grunt-contrib-watch');
    grunt.loadNpmTasks('grunt-contrib-uglify');
    

    Grunt does not take multiple Arguments in grunt.loadNpmTasks for some reason. You can see the proper usage of the loadNpmTasks - function in the documentation: https://gruntjs.com/sample-gruntfile