Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to install grunt and how to build script with it

Hi I'm trying to install Grunt on Windows 7 64 bit. I have installed Grunt using commands

 npm install -g grunt  npm install -g grunt-cli 

but now if I try to do grunt init, it is throwing me an error -

A valid Gruntfile could not be found. Please see the getting started guide for more information on how to configure grunt: http://gruntjs.com/getting-started Fatal error: Unable to find Gruntfile.

But when I look inside the grunt folder on my system the Gruntfile.js is there. can someone please guide me how to install this grunt properly and how to write built Script using the grunt. I have one HTML page and java script if i wants built a script using Grunt how can i do it?

like image 848
Sau Avatar asked Mar 29 '13 12:03

Sau


People also ask

How do I install grunt locally to my project?

Installing grunt-cli locally js method to get started with a project ( npm install && npm test ) then install grunt-cli locally with npm install grunt-cli --save-dev. Then add a script to your package. json to run the associated grunt command: "scripts": { "test": "grunt test" } .

Where do I put grunt?

Installing a published development version Like installing a specific version of grunt, run npm install grunt@VERSION --save-dev where VERSION is the version you need, and npm will install that version of Grunt in your project folder, adding it to your package.

Is grunt a build tool?

Overview. Grunt was originally created by Ben Alman in 2012 as an efficient alternative to simplify writing and maintaining a suite of JavaScript build process tasks in one huge file. It was designed as a task-based command line build tool for JavaScript projects.


2 Answers

To setup GruntJS build here is the steps:

  1. Make sure you have setup your package.json or setup new one:

    npm init 
  2. Install Grunt CLI as global:

    npm install -g grunt-cli 
  3. Install Grunt in your local project:

    npm install grunt --save-dev 
  4. Install any Grunt Module you may need in your build process. Just for sake of this sample I will add Concat module for combining files together:

    npm install grunt-contrib-concat --save-dev 
  5. Now you need to setup your Gruntfile.js which will describe your build process. For this sample I just combine two JS files file1.js and file2.js in the js folder and generate app.js:

    module.exports = function(grunt) {      // Project configuration.     grunt.initConfig({         concat: {             "options": { "separator": ";" },             "build": {                 "src": ["js/file1.js", "js/file2.js"],                 "dest": "js/app.js"             }         }     });      // Load required modules     grunt.loadNpmTasks('grunt-contrib-concat');      // Task definitions     grunt.registerTask('default', ['concat']); }; 
  6. Now you'll be ready to run your build process by following command:

    grunt 

I hope this give you an idea how to work with GruntJS build.

NOTE:

You can use grunt-init for creating Gruntfile.js if you want wizard-based creation instead of raw coding for step 5.

To do so, please follow these steps:

npm install -g grunt-init git clone https://github.com/gruntjs/grunt-init-gruntfile.git ~/.grunt-init/gruntfile grunt-init gruntfile 

For Windows users: If you are using cmd.exe you need to change ~/.grunt-init/gruntfile to %USERPROFILE%\.grunt-init\. PowerShell will recognize the ~ correctly.

like image 51
Qorbani Avatar answered Sep 23 '22 10:09

Qorbani


Some time we need to set PATH variable for WINDOWS

%USERPROFILE%\AppData\Roaming\npm

After that test with where grunt

Note: Do not forget to close the command prompt window and reopen it.

like image 28
Partha Sarathi Ghosh Avatar answered Sep 22 '22 10:09

Partha Sarathi Ghosh