$ sudo npm install -g js-beautify
Next, open up your emacs init file (~/.emacs.d/init.el) and add the following lisp code:
(defun jstidy ()
'Run js-beautify on the current region or buffer.'
(unless mark-active (mark-defun))
(shell-command-on-region (point) (mark) 'js-beautify -f -'; nil t)))
(global-set-key "\C-cg" 'jstidy)
Once you’ve re-evaluated or restarted emacs the (CTRL+c then g) will be bound to the jstidy function.
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.