unexpected identifier error in grunt.js file - gruntjs

Can anyone tell me why I am getting the following error in terminal when running the following grunt.js file?
module.exports = function(grunt) {
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
concat: { // Here is where we do our concatenating
dist: {
src: [
'components/js/*.js' // everything in the src js folder
],
dest: 'js/script.js',
}
}
uglify: {
build: {
src: 'js/script.js',
dest: 'js/script.min.js'
}
}
});
//Add all the plugins here
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-contrib-uglify');
//What happens when we type 'grunt' in the terminal
grunt.registerTask('default', ['concat', 'uglify']);
};
The error I get is:
Loading "gruntfile.js" tasks...ERROR
SyntaxError: Unexpected identifier
Warning: Task "default" not found. Use --force to continue.
THANKS!

You have a missing comma:
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
concat: {
dist: {
src: [
'components/js/*.js'
],
dest: 'js/script.js',
}
}, // <---- here was a missing comma
uglify: {
build: {
src: 'js/script.js',
dest: 'js/script.min.js'
}
}
});
Make yourself a favor and use coffeescript!
module.exports = (grunt) ->
grunt.initConfig
pkg: grunt.file.readJSON("package.json")
concat:
dist:
src: ["components/js/*.js"]
dest: "js/script.js"
uglify:
build:
src: "js/script.js"
dest: "js/script.min.js"
grunt.loadNpmTasks "grunt-contrib-concat"
grunt.loadNpmTasks "grunt-contrib-uglify"
grunt.registerTask "default", [
"concat"
"uglify"
]

Related

Task Default Not Found , despite mentioning the Default Task

There are questions ask before but I couldn't get much out of them as I'm new to grunt.Whenever I run code , Console reads as follows
SyntaxError: Invalid or unexpected token
Warning: Task "default" not found. Use --force to continue.
My code is:
'use strict';
module.exports = function (grunt) {
// Time how long tasks take. Can help when optimizing build times
require('time-grunt')(grunt);
// Automatically load required Grunt tasks
require('jit-grunt')(grunt, {
useminPrepare: 'grunt-usemin'
});
// Define the configuration for all the tasks
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
// Make sure code styles are up to par and there are no obvious mistakes
jshint: {
options: {
jshintrc: '.jshintrc',
reporter: require('jshint-stylish')
},
all: {
src: [
'Gruntfile.js',
'app/scripts/{,*/}*.js'
]
}
},
useminPrepare: {
html: 'app/menu.htm
},
useminPrepare: {
html: 'app/menu.html',
options: {
dest: 'dist'
}
},
// Concat
concat: {
options: {
separator: ';'
},
// dist configuration is provided by useminPrepare
dist: {}
},
// Uglify
uglify: {
// dist configuration is provided by useminPrepare
dist: {}
},
cssmin: {
dist: {}
},
// Filerev
filerev: {
options: {
encoding: 'utf8',
algorithm: 'md5',
length: 20
},
release: {
// filerev:release hashes(md5) all assets (images, js and css )
// in dist directory
files: [{
src: [
'dist/scripts/*.js',
'dist/styles/*.css',
]
}]
}
},
// Usemin
// Replaces all assets with their revved version in html and css files.
// options.assetDirs contains the directories for finding the assets
// according to their relative paths
usemin: {
html: ['dist/*.html'],
css: ['dist/styles/*.css'],
options: {
assetsDirs: ['dist', 'dist/styles']
}
},
copy: {
dist: {
cwd: 'app',
src: [ '**','!styles/**/*.css','!scripts/**/*.js' ],
dest: 'dist',
expand: true
},
fonts: {
files: [
{
//for bootstrap fonts
expand: true,
dot: true,
cwd: 'bower_components/bootstrap/dist',
src: ['fonts/*.*'],
dest: 'dist'
}, {
//for font-awesome
expand: true,
dot: true,
cwd: 'bower_components/font-awesome',
src: ['fonts/*.*'],
dest: 'dist'
}
]
}
},
clean: {
build: {
src: [ 'dist/']
}
}
});
grunt.registerTask('build', [
'clean',
'jshint',
'useminPrepare',
'concat',
'cssmin',
'uglify',
'copy',
'filerev',
'usemin'
]);
grunt.registerTask('default' , ['build']);
};
the complete output of your grunt call shows that your gruntfile.js has a sytax error:
Loading "gruntfile.js" tasks...ERROR
>> SyntaxError: Invalid or unexpected token Warning: Task "default" not found. Use --force to continue.
Aborted due to warnings.
and indeed your file has no closing quotes on line 37:
useminPrepare: {
html: 'app/menu.htm <-----!!!!!!!!
},
useminPrepare: {
html: 'app/menu.html',
options: {
dest: 'dist'
}
},
probably a copy paste error since you have the useminPrepare twice (which is illegal in strict mode btw...)

How to run two different Grunt task in one command?

I am using 'cssmin' and 'sass' grunt task separately, Is it possible to run them one after other automatically, when I can anything in my file.
cssmin: {
target: {
files: [{
cwd: srcCss,
src: ['**/*.css', '*.css'],
dest: buildCss
}]
}
},
sass: {
target: {
files: [{
cwd: srcScss,
src: ['**/*.scss', '*.scss'],
dest: srcCss
}]
}
}
This can be done easily: You need to use watch plugin,
npm install grunt-contrib-watch
grunt.loadNpmTasks('grunt-contrib-watch');
3.
grunt.initConfig({
watch: {
scripts: {
files: ['lib/*.js'],
tasks: ['jshint'],
options: {
spawn: false,
},
},
},
jshint: {
all: {
src: ['lib/*.js'],
},
},
});
// Default task(s).
grunt.registerTask('default', 'watch');
Now Just run grunt in cmd.

task jade not found when running grunt

When I run grunt with the following Gruntfile, I get the error Warning: task "jade" not found. what exactly could be wrong here?
module.exports = function(grunt) {
grunt.initConfig({
jade: {
compile: {
options: {
client: false,
pretty: true
},
files: [ {
cwd: "app/views",
src: "**/*.jade",
dest: "build/templates",
expand: true,
ext: ".html"
} ]
}
}
});
grunt.registerTask('default','Convert Jade templates into html templates',
['jade','watch']);
grunt.loadNpmTasks('grunt-contrib-watch');
};
You need to load grunt jade task. The same as yo are adding grunt-contrib-watch
grunt.loadNpmTasks('grunt-contrib-jade');
This should works.

Grunt is not working on windows

here is my code, the error is:
Warning: task "concat" not found
module.exports = function(grunt){
grunt.initConfig({
concat: {
options: {
separator: ';',
},
dist: {
src: ['calculation/index.js', 'calculation/test.js'],
dest: 'dist/built.js',
},
},
});
grunt.loadNpmTask("grunt-contrib-contat");
grunt.registerTask('default',['concat'])
;};
I believe you simply need to move the registration of your grunt-contrib-concat task above the point where the config is looking for it.

Extend sails 0.10 grunt set-up

I am intending to add a task to sails's (0.10 rc5) grunt build system.
tasks/config/asset-versioning.js
module.exports = function(grunt) {
grunt.config.set('asset-versioning', {
copy: {
src: '.tmp/public/min/production.js',
dest: '.tmp/public/min/production2.js'
}
});
grunt.task.registerTask('asset-versioning', ['copy']);
grunt.loadNpmTasks('grunt-contrib-copy');
};
tasks/register/prod.js
module.exports = function (grunt) {
grunt.registerTask('prod', [
'compileAssets',
'concat',
'uglify',
'cssmin',
'sails-linker:prodJs',
'sails-linker:prodStyles',
'sails-linker:devTpl',
'asset-versioning' // Added the task config here
]);
};
After running sails lift --prod --verbose I am getting following error:
Grunt :: Warning: Task "asset-versioning" not found.
** Grunt :: An error occurred. **
What am I missing?
EDIT
Apparently I am missing to register my task.
Adding grunt.task.registerTask('asset-versioning'); to the first file. I am not getting any error, but my task is not running! Nothing is happening.
Okay, you're having this all mixed up. Copy is a predefined task, you just need to tweak its configs. Go to tasks/config/copy.js and add your custom configuration (in the grunt.config.set call)
productionfiles: {
src: '.tmp/public/min/production.min.js',
dest: '.tmp/public/min/production2.min.js'
}
Then, in the tasks/register/prod.js, make sure you call copy:productionfiles, and it will do the magic.
Copy.js
module.exports = function(grunt) {
grunt.config.set('copy', {
dev: {
files: [{
expand: true,
cwd: './assets',
src: ['**/*.!(coffee|less)'],
dest: '.tmp/public'
}]
},
build: {
files: [{
expand: true,
cwd: '.tmp/public',
src: ['**/*'],
dest: 'www'
}]
},
productionfiles: {
src: '.tmp/public/min/production.min.js',
dest: '.tmp/public/min/production2.min.js'
}
});
grunt.loadNpmTasks('grunt-contrib-copy');
};
Prod.js
module.exports = function (grunt) {
grunt.registerTask('prod', [
'compileAssets',
'concat',
'uglify',
'cssmin',
'sails-linker:prodJs',
'sails-linker:prodStyles',
'sails-linker:devTpl',
'sails-linker:prodJsJade',
'sails-linker:prodStylesJade',
'sails-linker:devTplJade',
'copy:productionfiles'
]);
};

Resources