Operating System Debian

How to Install Grunt (GruntJS) on Debian 13

Install Node.js and grunt-cli on Debian 13, add Grunt to a project as a development dependency and run a first task from a Gruntfile.

Grunt is a JavaScript task runner built on Node.js. You describe repetitive jobs such as minifying, concatenating, linting or running tests in a file called Gruntfile.js, and Grunt runs them with one command. This guide installs Node.js, npm and the Grunt command line on Debian 13, then sets up a small project with a working Gruntfile. The same steps apply to Debian 12; the only difference is the Node.js version the distribution ships.

Grunt is still available and its repository is not archived, but development is slow: the 1.6 series dates from January 2023. It remains a reasonable choice for maintaining existing build setups. Debian 9 itself is end-of-life, and so is Node.js 8 that older tutorials install, so both are replaced here with current packages.

Requirements

  • A server or workstation running Debian 13 (or Debian 12).
  • A non-root user with sudo rights. This guide uses deploy.

Step 1: Install Node.js and npm

Debian 13 ships Node.js 20 and npm in its own repositories, which is more than enough: the current Grunt release requires Node.js 16 or newer and grunt-cli requires Node.js 10 or newer. Debian 12 ships Node.js 18, which also meets both requirements.

sudo apt update
sudo apt install -y nodejs npm

Check the versions:

node --version
npm --version

If a project needs a newer Node.js line than Debian provides, install that first by the method you prefer; nothing else in this guide changes.

Step 2: Install the Grunt command line

Grunt comes in two parts. grunt-cli is a small launcher that puts the grunt command on your path. It does not contain the task runner itself; each project installs its own copy of grunt, and the launcher finds and runs that local copy. This lets different projects on one machine use different Grunt versions.

Install the launcher globally:

sudo npm install -g grunt-cli

Confirm that it is available:

grunt --version

Outside a project the command prints only the grunt-cli version. Inside a project it also prints the version of the local Grunt.

Step 3: Add Grunt to a project

A Grunt project needs two files in its root directory: package.json, which npm uses to record the project's dependencies, and Gruntfile.js, which configures and defines the tasks. Work as your normal user from here on. Running npm with sudo inside a project leaves root-owned files behind that you cannot edit later.

Create a project directory with a source folder:

mkdir -p ~/myproject/src
cd ~/myproject

Create package.json. The -y flag accepts the defaults; leave it out if you want to answer the questions yourself:

npm init -y

Install Grunt and one plugin as development dependencies. The plugin used here, grunt-contrib-uglify, minifies JavaScript:

npm install --save-dev grunt grunt-contrib-uglify

Both packages are now listed under devDependencies in package.json and installed in node_modules. Add a small file to minify:

cat > src/app.js <<'EOF'
function greet(name) {
  var message = 'Hello, ' + name + '!';
  console.log(message);
}
greet('world');
EOF

Step 4: Write the Gruntfile

Create Gruntfile.js in the project root:

nano Gruntfile.js

Add the following content:

module.exports = function(grunt) {

  // Project configuration
  grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    uglify: {
      build: {
        src: 'src/app.js',
        dest: 'build/app.min.js'
      }
    }
  });

  // Load the plugin that provides the "uglify" task
  grunt.loadNpmTasks('grunt-contrib-uglify');

  // A custom task defined inline
  grunt.registerTask('hello', 'Print a greeting', function() {
    grunt.log.writeln('Hello from Grunt in ' + grunt.config('pkg.name'));
  });

  // Tasks that run when you type "grunt" without arguments
  grunt.registerTask('default', ['hello', 'uglify']);

};

Every Gruntfile has the same shape: a wrapper function assigned to module.exports, the configuration passed to grunt.initConfig(), plugins loaded with grunt.loadNpmTasks(), and task names registered with grunt.registerTask().

Step 5: Run the tasks

Run the default task list:

grunt

Grunt prints the greeting, runs uglify and reports that it created build/app.min.js. You can also run a single task by name, or list everything the project offers:

grunt hello
grunt uglify
grunt --help

Working with existing projects and updates

When you clone a project that already has a package.json and a Gruntfile, install its dependencies and run Grunt; nothing else is needed:

cd /path/to/project
npm install
grunt

To see which of a project's packages have newer releases, and to update them within the version ranges in package.json:

npm outdated
npm update

Update the global launcher by installing it again:

sudo npm install -g grunt-cli

More plugins and the full configuration reference are in the Grunt getting started guide.

Frequently asked questions

Why does Grunt say it is unable to find a local grunt?

The global grunt command is only a launcher. It looks for a copy of Grunt installed in the current project and stops if there is none. Run npm install if the project already lists Grunt in package.json, or npm install --save-dev grunt if it does not.

Can I use Grunt without a global install?

Yes. With grunt installed as a development dependency, npx grunt runs the project's own copy. You can also add a line such as "build": "grunt" to the scripts section of package.json and call it with npm run build. Both avoid sudo and are convenient on build servers.

Should I commit node_modules to version control?

No. Commit package.json, package-lock.json and Gruntfile.js, and add node_modules and generated output such as build to .gitignore. Anyone can recreate the dependencies with npm install.

Is Grunt still a good choice for a new project?

It works and its plugins still install, but releases have been rare since early 2023. For an existing Grunt build there is no urgency to move. For a new project, npm scripts or a modern bundler usually need less configuration.

Share story:

Continue reading other guides on How VPS.