Minify JavaScript Using Terser

Terser is a javascript compressor and mangler supporting ES6+ specification. In this tutorial, you will get to know how to use terser to minify or compress javascript.

Prerequisite: nodejs should be installed on your system.

1. Installing terser

Install terser using yarn or npm.

npm i terser -g

2. Creating a script

Add the script to package.json file of your project.

"scripts": {
"minify": "terser src/js/app.js -c -m --output src/minified/app.min.js"

Replace src/js/app.js and src/minified/app.min.js with your input and output file path respectively.

Here, -c flag stands for --compress and -m stands for --mangle. For more options, visit the official documentation of terser.

3. Executing the script

Run the script using this command:

npm run minify

Your javascript file should now be compressed!


Apart from terser, you can also use to compress or minify javascript.

You can also try terser on the web.

Originally published at on August 8, 2022.



Full Stack Developer | Content Creator | Technical Writer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store