Loading "gruntfile.js" tasks...ERROR >> ReferenceError: grunt is not defined - gruntjs

there is a problem in my code.
I wrote in concole: grunt connect
This is the errormessage:
Loading "gruntfile.js" tasks...ERROR
ReferenceError: grunt is not defined
Warning: Task "connect" not found. Use --force to continue.
Aborted due to warnings.
And this is my gruntfile.js file:
module.exports = function(grunt) {
// Project configuration.
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
connect: {
uses_defaults: {}
}
});
// Load Grunt plugins
grunt.loadNpmTasks('');
// Default task(s).
grunt.registerTask('default', []);
};
// Load Grunt plugins
grunt.loadNpmTasks('grunt-contrib-connect');
What is the problem in my code?
Why doesn't it find the connect task?

"Every Gruntfile (and gruntplugin) uses this basic format, and all of your Grunt code must be specified inside this function:" -https://gruntjs.com/getting-started
module.exports = function(grunt) {
// Do grunt-related things in here
};
Moving grunt.loadNpmTasks('grunt-contrib-connect'); inside this function should fix it.

Related

Get the grunt-php to work

I am trying to get the php server to work.
I try to keep the code as simple as possible to make it work first. I have a folder in my D drive called firethis and it contains index.php. When i tell grunt-php to run, it gives me error. What am i doing wrong.
Thanks!
D:\>grunt php
Warning: Task "php" not found. Use --force to continue.
Aborted due to warnings.
config
module.exports = function(grunt){
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
php: {
dist: {
options: {
base: 'firethis'
}
}
},
grunt.loadNpmTasks('grunt-php');
grunt.registerTask('default', 'php');

What is wrong with Grunt's simple example file from official site?

I tried to set up grandfile.js, did it under http://gruntjs.com/configuring-tasks and https://24ways.org/2013/grunt-is-not-weird-and-hard/ . So I wrote there:
module.exports = function(grunt) {
// 1. All configuration goes here
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
concat: {
// 2. Configuration for concatinating files goes here.
dist: {
src: [
'css/bootstrap.css', // Bootstrap css in the libs folder
'css/normalize.css', // Normaliza.css in the libs folder
'css/style.css' // This specific file
],
dest: 'css/build/production.css',
}
});
// 3. Where we tell Grunt we plan to use this plug-in.
grunt.loadNpmTasks('grunt-contrib-concat');
// 4. Where we tell Grunt what to do when we type "grunt" into the terminal.
grunt.registerTask('concat');
};
Then I opened Teminal and wrote grunt.
Here what I got:
Loading "Gruntfile.js" tasks...ERROR
>> SyntaxError: Unexpected token )
Warning: Task "default" not found. Use --force to continue.
Aborted due to warnings.
I've not find any extra ) neither I create bad "default" task (default=concat).
What is wrong here?
Thanks.
Grunt expects you to register a default task (grunt.registerTask('default', [<task list here>]), but you do not have to. If you don't wish to register it, then just pass the --force option like it mentions, and it should still run your task(s)

Looking for Modernizr references

I'm trying to use the grunt-modernizr plugin in my project but I'm receiving the following output when I run tasks:
Running "modernizr:dist" (modernizr) task
>> Explicitly including these tests:
>> pointerevents
Looking for Modernizr references
I'm not receiving any type of error the terminal just goes back to the directory that I'm in, as if it's just giving up.
Here is my grunt file:
module.exports = function(grunt) {
grunt.initConfig ({
// Do grunt-related things in here
pkg: grunt.file.readJSON('package.json'),
modernizr: {
dist: {
"dest": "javascripts/modernizr-custom.js",
"parseFiles": true,
"customTests": [],
"devFile": "javascripts/modernizr-custom.js",
"outputFile": "javascripts/min/modernizr-custom.min.js",
"tests": [
"pointerevents",
"css/pointerevents"
],
"extensibility": [
"setClasses"
],
"uglify": false
}
},
cssmin: {
target: {
files: {
'css/min/bootstrap.min.css': ['css/bootstrap.css']
}
}
},
});
grunt.loadNpmTasks("grunt-modernizr");
grunt.loadNpmTasks('grunt-contrib-cssmin');
grunt.registerTask('default',['modernizr', 'cssmin']);
};
Output from running grunt --verbose:
Initializing
Command-line options: --verbose
Reading "gruntfile.js" Gruntfile...OK
Registering Gruntfile tasks.
Reading package.json...OK
Parsing package.json...OK
Initializing config...OK
Registering "grunt-modernizr" local Npm module tasks.
Reading /Applications/MAMP/htdocs/bootstrap-three-wordpress/wp-content/themes/brandozz/node_modules/grunt-modernizr/package.json...OK
Parsing /Applications/MAMP/htdocs/bootstrap-three-wordpress/wp-content/themes/brandozz/node_modules/grunt-modernizr/package.json...OK
Loading "modernizr.js" tasks...OK
+ modernizr
Registering "grunt-contrib-cssmin" local Npm module tasks.
Reading /Applications/MAMP/htdocs/bootstrap-three-wordpress/wp-content/themes/brandozz/node_modules/grunt-contrib-cssmin/package.json...OK
Parsing /Applications/MAMP/htdocs/bootstrap-three-wordpress/wp-content/themes/brandozz/node_modules/grunt-contrib-cssmin/package.json...OK
Loading "cssmin.js" tasks...OK
+ cssmin
Loading "gruntfile.js" tasks...OK
+ default
No tasks specified, running default tasks.
Running tasks: default
Running "default" task
Running "modernizr" task
Running "modernizr:dist" (modernizr) task
Verifying property modernizr.dist exists in config...OK
Files: -> javascripts/modernizr-custom.js
Verifying property modernizr exists in config...OK
>> Explicitly including these tests:
>> pointerevents
Looking for Modernizr references
This is something I just came across too and seems to be grunt-modernizr stopping after customizr doesn't find any files to crawl (it crawls by default).
If you add "crawl": false to your modernizr:dist task that should fix the problem.
Also, I think "extensibility": [ "setClasses" ], should be "options": [ "setClasses" ],.
To use the grunt-modernizr task to crawl your code for Modernizr references you'll have to look at the config properties for the customizr task as this is part of grunt-modernizr 's node_modules:
modernizr: {
dist: {
dest: 'bower_components/modernizr/build/modernizr.custom.js',
uglify: false,
options: [
'setClasses',
'addTest'
],
files: {
src: ['js/app/**/*.js', 'js/app/*.js']
}
}
}
devFile: doesn't seem to matter where you point at
dest: instead of outputFile, note I'm just outputting to a build directory that's not part of the package
uglify: false if you have other minifying options like bundleconfig.json
options: to bypass the default options { "setClasses", "addTest", "html5printshiv", "testProp", "fnBind" }
files: to enlist your crawlable director(y|ies), make sure you take care of the root files and/or subdirectories as well
Load the required tasks, in my case:
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-modernizr');
grunt.loadNpmTasks('grunt-contrib-copy');
Refer to the 'modernizr:dist' task => grunt.registerTask('default', ['clean', 'modernizr:dist', 'copy']);
Which results in an unminified 34kb file:
Running "clean:files" (clean) task
19 paths cleaned.
Running "modernizr:dist" (modernizr) task
Looking for Modernizr references
1 match in js/app/classes/yambo.options.js
bgpositionxy
1 match in js/app/modules/yambo.audio.js
audio
Ready to build using these settings:
setClasses, addTest
Building your customized Modernizr...OK
Success! Saved file to bower_components/modernizr/build/modernizr.custom.js
Process terminated with code 0.
Running "copy:main" (copy) task
Copied 11 files
Done, without errors.
This way there's no need to even go to the online build to add a feature test. Simply reference Modernizr throughout your js code:
window.Yambo = (function($, modernizr, ns){
ns.Audio = {
extension: (function () {
return modernizr && modernizr.audio.mp3
? 'mp3'
: modernizr.audio.ogg
? 'ogg'
: 'wav';
}())
};
return ns;
}(window.jQuery, window.Modernizr, window.Yambo || {}));
Make sure to use the correct property name for a feature detection, so customizr can pick it up and provide a test to your custom build.
This should be also possible for css but haven't been testing that for the moment.
It looks like you missed source files.
http://gruntjs.com/configuring-tasks#files-object-format
Try to include
"dist": {
"files": {
"src": ['!<%= appDir %>assets/js/bower/modernizr/**']
}
}

Sending specs in grunt using grunt-protractor-runner

I am using grunt-protractor-runner plugin and in the protractor target I want to send the specs param containing the test to run.
In the grunt file my target looks as follows:
testIntegration:
{
options:
{
args: {
specs: ['test1.js'],
browser: 'firefox'
}
}
The protractor parent task option contains setting of the protractor config file.
When running this target I get this error:
$ grunt protractor:testIntegration
Running "protractor:testIntegration" (protractor) task
Starting selenium standalone server...
Selenium standalone server started at ...
Warning: pattern t did not match any files.
Warning: pattern e did not match any files.
Warning: pattern s did not match any files.
Warning: pattern t did not match any files.
Warning: pattern 1 did not match any files.
Warning: pattern j did not match any files.
Warning: pattern s did not match any files.
and then some more errors.
the same line works well in Protractor config file.
Tried a few other variation but no success.
What am I missing? Any ideas?
Try this configuration:
module.exports = function(grunt) {
// Project configuration
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
protractor: {
options: {
keepAlive: true,
singleRun: false,
configFile: "PROTRACTOR_CONFIG_FILE.js"
},
run_firefox: {
options: {
args: {
browser: "firefox"
}
}
}
});
// load grunt-protractor-runner
grunt.loadNpmTasks('grunt-protractor-runner');
// register tasks
grunt.registerTask('default', 'Run Protractor using Firefox',
['protractor:run_firefox']);
};
Funny, if you read every error message, it spells out "test1.js". Looks like it's not reading in the config file correctly, probably because you're not using grunt.file.readJSON('FILENAME.json')

Grunt watch throws NoMethodError

I've read the grunt, sass, and compass documentation but can't find much about this error. I also saw another person with a similar error but it was due to syntax problems in their contrib.rb file, which I don't believe I have in my contrib.rb file.
Problem: I run the grunt watch task and get an error anytime I make a change to my sass (*.scss) files.
The error reads as follows:
NoMethodError on line 264 of /Library/Ruby/Gems/1.8/gems/compass-0.12.2/lib/compass/configuration/inheritance.rb: _
Run with --trace to see the full backtrace
As a result, none of my sass files are processing to my css folder. I'm new to grunt and am admittedly a little overwhelmed with the amount of initial setup I've done. I'd love any advice as to how my files might be configured incorrectly or whether there might be an error with one of the versions of compass or sass as it relates to grunt (all are most recent non-beta versions).
Here is the text of my contrib.rb file:
require "susy"
css_dir = _/css
sass_dir = _/components/sass
javascripts_dir = _/js
output_style = :compressed
My gruntfile.js :
module.exports = function(grunt) {
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-compass');
grunt.initConfig({
uglify: {
my_target: {
files: {
'_/js/script.js': ['_/components/js/*.js'] //tells grunt to track and compress any .js file found in components/js folder
} //files
} // my_target
}, // uglify
compass: {
dev: {
options: {
config: 'config.rb',
force: true // <-- ???
} //options
} //dev
}, //compass
watch: {
options: { livereload: true },
scripts: {
files: ['_/components/js/*.js'],
tasks: ['uglify']
}, //scripts
sass: {
files: ['_/components/sass/*.scss'],
tasks: ['compass:dev']
}, //sass
html: {
files: ['*.html']
}
} // watch
})// initConfig
grunt.registerTask('default', 'watch'); //makes 'grunt' command automaticaly execute the watch command to monitor JS files
} //exports
And my project folder structure:
sass_first_proj
|
_(folder)
|
components
|
css
|
js
images
node_modules
|
grunt
|
grunt-contrib-compass
|
grunt-contrib-uglify
|
grunt-contrib-watch
|
matchdep
.gitignore
|
config.rb
|
gruntfile.js
|
index.html
|
package.json
Not sure if you were able to solve this, but I was receiving the same error.
I had to include a forward slash before the directories to get Grunt to the right paths. I'm using similar steps in using Grunt and Compass for my project, and I'm on my PC so I just put the paths in single quotes. Based on how your project directory is structured, the two respective lines in the config.rb file would look like this:
css_dir = '/_/css'
sass_dir = '/_/components/sass'
Also in your question, I noticed you stated it was the "contrib.rb" file, but in your file directory that it is named config.rb. You reference it in your gruntfile.js file as config.rb, as well, but I just wanted to make sure that this file in the root of your project was being named "config.rb".
I know it was a lot of setup, so I hope this works for you.
So here's the thing, I've never used a contrib.rb file when using compass with Grunt. I know you can, and if you get it working, great. but the other option is to just specify the paths in the grunt config. Of course, this all assumes that you are able to actually compile the sass files. When googling for that error, it seems that much of the time it was an issue with the contrib.rb file that caused the issue.
So with that said, maybe you could leave off the contrib.rb file and just use Grunt?
compass: {
dev: {
options: {
sassDir: "_/components/sass",
specify: "_/components/sass/**/*.scss", // might not be necessary for you
cssDir: "_/css",
javascriptsPath: "_/js",
outputStyle: "compressed",
require: "susy"
}
}
}

Resources