How to install grunt on Ubuntu 14.04
Grunt is a JavaScript task runner that can be used to compile Sass, run JSHint, or run many other plugins. It depends on the Node.js package manager, npm.
If you use the standard Ubuntu Trusty Tahr repository to install nodejs/npm, you will get this error when running grunt:
/usr/bin/env: node: No such file or directoryInstead, use the chris-lea nodejs repository.
Install nodejs, npm, and grunt-cli¶
$ sudo add-apt-repository ppa:chris-lea/node.js
$ sudo apt-get update
$ sudo apt-get install nodejs
$ sudo npm install -g grunt-cli
Install grunt in your project directory¶
$ cd ~/myproject
$ echo "{}" > package.json
$ npm install grunt --save-dev
Verify grunt is installed¶
$ nodejs --version
v0.10.33
$ npm --version
1.4.28
$ grunt --version
grunt-cli v0.1.13
grunt v0.4.5
Run a simple grunt task¶
$ cd ~/myproject
- Create a package.json file:
{ "name": "my-project-name", "version": "0.1.0", "devDependencies": { "grunt": "~0.4.5", "grunt-contrib-uglify": "~0.5.0" } }
- Install grunt-contrib-uglify
$ npm install npm WARN package.json [email protected] No description npm WARN package.json [email protected] No repository field. npm WARN package.json [email protected] No README data [email protected] node_modules/grunt-contrib-uglify ├── [email protected] ([email protected], [email protected], [email protected], [email protected], [email protected]) ├── [email protected] ├── [email protected] ([email protected], [email protected], [email protected]) └── [email protected] ([email protected], [email protected], [email protected], [email protected])
- Get an example unminified JS file:
$ wget http://code.jquery.com/jquery-2.1.1.js --2014-11-22 00:47:31-- http://code.jquery.com/jquery-2.1.1.js Resolving code.jquery.com (code.jquery.com)... 94.31.29.53, 94.31.29.230 Connecting to code.jquery.com (code.jquery.com)|94.31.29.53|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 247351 (242K) [application/x-javascript] Saving to: ‘jquery-2.1.1.js’ 100%[================================================================================================================>] 247,351 --.-K/s in 0.1s 2014-11-22 00:47:31 (1.71 MB/s) - ‘jquery-2.1.1.js’ saved [247351/247351]
- Create a Gruntfile.js file:
module.exports = function(grunt) { grunt.initConfig({ pkg: grunt.file.readJSON('package.json'), uglify: { build: { src: 'jquery-2.1.1.js', dest: 'jquery-2.1.1.min.js' } } }); grunt.loadNpmTasks('grunt-contrib-uglify'); grunt.registerTask('default', ['uglify']); };
- Run the grunt task:
$ grunt Running "uglify:build" (uglify) task Done, without errors.
- You should now have a minified file, jquery-2.1.1.min.js
$ ls -gG jquery* -rw-rw-r-- 1 247351 2014 10/23 17:16 jquery-2.1.1.js -rw-rw-r-- 1 84113 2014 11/22 00:48 jquery-2.1.1.min.js
References
Related posts
- How to use ast-grep with GraphQL — posted 2024-09-24
- Next.js App Router (RSC) projects w/ open source code — posted 2024-07-30
- Next.js Relay GraphQL Pokemon example — posted 2024-05-22
- Example Node.js Passport.js SAML app using OneLogin — posted 2024-05-10
- Aphrodite to CSS Modules codemod — posted 2022-12-09
- Simple codemod example with jscodeshift — posted 2021-05-03
Comments
You could alternatively install nodejs-legacy
. This stackoverflow answer discusses the reason for the extra package.
Bradford, thanks for the tip and link. I guess that is a more stable alternative.
Thanks your tutorial,, I wonder How can make package file (.json) in ubuntu
disqus:1872122751
Here's some information about creating the package.json file: http://gruntjs.com/getting-...
disqus:1872188296
The cause of the error "/usr/bin/env: node: No such file or directory" is because it is looking for nodejs under /usr/bin/node.
If you make a symbolic link for node (i.e. sudo ln -s /usr/bin/nodejs /usr/bin/node) the error should resolve and you can install grunt directly from: 'npm install -g grunt-cli' and 'npm install grunt'
disqus:2010620127
Curious, do you know how I can get Grunt to auto-run when I restart my server?
disqus:2554138664
Thank you a million times. You shall live long.
disqus:2770338961
Thanks Devin, you're awesome!
disqus:2979898070
installed npm and nodejs but, grunt is not installed.
when i execute command grunt --version nothing is in the putput.
disqus:3070661324
You should add
sudo apt-get install npm
too in the install nodejs section..
disqus:3178634907