2

I'm new to Grunt and I'm having trouble setting up a project with new plugins.

So far I have:

  1. installed grunt-cli globally
  2. installed grunt-init globally
  3. downloaded the gruntfile template from the GIT repository
  4. created a new directory 'test' and navigated to the directory in Terminal
  5. created a new grunt project from the template (grunt-init gruntfile)
  6. installed the project dependencies (npm install)

This all works great, however I want to use a different uglify plugin so, still in the 'test' directory, I run:

npm install uglify-js --save-dev 

As I'm not using the global flag, the plugin downloads to test/node_modules directory as expected, existing at test/node_modules/uglify-js. The package.json file has been updated with the additional dependency:

"uglify-js": "^2.4.15" 

I then edit Gruntfile.js, adding

grunt.loadNpmTasks('uglify-js'); 

And change

grunt.registerTask('default', ['jshint', 'qunit', 'concat', 'uglify']); 

To

grunt.registerTask('default', ['concat', 'uglify-js']); 

I run

npm install 

to ensure all dependencies are downloaded, but now when I run the grunt task I get this error:

Local Npm module "uglify-js" not found. Is it installed? Warning: Task "uglify-js" not found. Use --force to continue. 

Anyone have any idea what I'm doing wrong?

Mac OS X 10.8.5, Node v0.10.32

2 Answers 2

2

If you want to use UglifyJS whit grunt, you should use grunt-contrib-uglify. Install it with:

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

enable it inside your Gruntfile.js:

grunt.loadNpmTasks('grunt-contrib-uglify'); 

and run your task:

grunt uglify 

See the docs for more information.

Sign up to request clarification or add additional context in comments.

2 Comments

I should have specified that the grunt-contrib-uglify plugin is included with the gruntfile template. I was under the impression that an option I was passing in (compress: {drop_console: true}) was not working, when in fact it was… My original question is more theoretical now - was grunt complaining because the uglify-js module is a generic Node module, not a Grunt plugin?
Yes @Terry. When you want to automate the use of a npm module with grunt.js, it's desirable that you use (or write your own if it doesn't exists) a plugin. Maybe you could use an npm module without a plugin, but you would have to do a lot of unnecessary configuration, which is already done by the plugin. I've never tried that.
1

I had a similar issue on windows 7. I resolved this issue by creating a package.json with the below modules.

"grunt": "~0.4.5", "grunt-contrib-jshint": "~0.10.0", "grunt-contrib-nodeunit": "~0.4.1", "grunt-contrib-uglify": "~0.5.0" 

Ran npm install and it started working within that directory.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.