^
SyntaxError: Unexpected token {
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:374:25)
at Object.Module._extensions..js (module.js:417:10)
at Module.load (module.js:344:32)
at Function.Module._load (module.js:301:12)
at Function.Module.runMain (module.js:442:10)
at startup (node.js:136:18)
at node.js:966:3
The error you get from Node may be different, as Node is in the process of imple‐
menting ES6 features (if you’re reading this book far enough in the future, it may
work completely!). Now let’s run the ES5 equivalent:
$ node dist\test.js
JavaScript is great
Elephants are large
We’ve successfully converted ES6 code to portable ES5 code, which should run any‐
where! As a last step, add dist and public/dist to your .gitignore file: we want to keep
track of the ES6 source, not the ES5 files that are generated from it.
Linting
Do you run a lint roller over your dress or suit before you go to a fancy party or an
interview? Of course you do: you want to look your best. Likewise, you can lint your
code to make it (and by extension, you) look its best. A linter takes a critical eye to
your code and lets you know when you’re making common mistakes. I’ve been writ‐
ing software for 25 years, and a good linter will still find mistakes in my code before I
do. For the beginner, it’s an invaluable tool that can save you a lot of frustration.
There are several JavaScript linters out there, but my preference is Nicholas Zakas’s
ESLint. Install ESLint:
npm install -g eslint
Before we start using ESLint, we need to create an .eslintrc configuration file for our
project. Each project you work on may have different technologies or standards, and
the .eslintrc allows ESLint to lint your code accordingly.
The easiest way to create an .eslintrc file is to run
eslint --init
, which will interac‐
tively ask you some questions and create a default file for you.
In your project root, run
eslint --init
. The answers you will need to have are:
Linting | 27