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.

Sarjan Desai
  • 3,683
  • 2
  • 19
  • 32
  • 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? – azium Oct 15 '15 at 04:58

1 Answers1

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});
Sarjan Desai
  • 3,683
  • 2
  • 19
  • 32
  • i installed bower-requirejs and i injected bower-requirejs again it will throw same error 'require is undefined' – SatyanarayanaR Oct 15 '15 at 05:44
  • this is my code: var UglifyJS = require("uglify-js"); var result = UglifyJS.minify("var b = function () {};", { fromString: true }); – SatyanarayanaR Oct 15 '15 at 06:20
  • Got your issue. You are trying to minify from client side but above code work for server...For client side, use following [link](http://stackoverflow.com/questions/17424728/uglifyjs-on-client-side-javascript-or-an-alternative-parser) – Sarjan Desai Oct 15 '15 at 06:31