1

This is my code, i tried the following code but not working

var UglifyJS = require("uglify-js");
var result = UglifyJS.minify("Hello world @123;", { fromString: true });
console.log(result.code);

In the above code i am using UglifyJs. but i got error like require is undefined. i want to minimize the above string by using angularjs.

1
  • To use require you probably want to use a module loader like browserify. Also.. what does it mean to minify a string? What could the output possibly be? Commented Oct 15, 2015 at 4:58

1 Answer 1

1
var UglifyJS = require("uglify-js");

This code get the uglify-js and assign it to variable.

The error require is undefined is possible because of not inserting requirejs in your project.

Install requirejs using npm in your project directory

$ npm install --save bower-requirejs

Check more details on : https://github.com/yeoman/bower-requirejs

Notes:

UglifyJS is a JavaScript parser, minifier, compressor or beautifier toolkit. It is not used to minifying string.

Below is basic example which has javascript code not string.

var result = UglifyJS.minify("var b = function () {};", {fromString: true});
Sign up to request clarification or add additional context in comments.

3 Comments

i installed bower-requirejs and i injected bower-requirejs again it will throw same error 'require is undefined'
this is my code: var UglifyJS = require("uglify-js"); var result = UglifyJS.minify("var b = function () {};", { fromString: true });
Got your issue. You are trying to minify from client side but above code work for server...For client side, use following link

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.