How to Format JavaScript Code from Command Line

You can use jsbeautifier library to format your JavaScript code from the command line.

To use this library, install it first by running the following command in the command-line window.

$ pip install jsbeautifier

This assumes you have pip installed.

Then use it on a JavaScript file by calling (assuming the file is in the same folder as your command line window)

$ js-beautify file_name.js

Notice that this will not make the changes. It only shows what would change.

To actually make the changes, run this command:

$ js-beautify -r file_name.js

Now you can inspect your JavaScript file to see that its formatting has changed to follow the best practices.


Thanks for reading. I hope you find it useful.

Happy coding!

Further Reading

100 JavaScript Interview Questions

50 Buzzwords of Web Development