Skip to the content.

A guide for ES2018 migration

Write code in modern JavaScript!

This manual is meant for server-side code, but is largely valid for the whole application.

Tell eslint

Let’s assume you (re)write a file ‘modules/users/server/somefile.js’ in ES2018.

Open .eslintrc.js. Find a section overrides. Add your file to files in overrides for server code.

// ...
overrides: [{
  // ...other overrides
}, {
  // overrides for server code
  // ES2018 - specify migrated files and folders here
  files: [
    ...
    'modules/users/server/somefile.js', // add the path to your file here
    ...
  ]
}]

Note: You can add whole folders. See the .eslintrc.js for examples.

Promises and async/await

Replace async library and callback hell with Promises and async/await.

Try to find out whether your library of choice offers promise versions of library methods. Study the documentation how to use them. For example many methods in mongoose library support Promises.

If only a callback method is available, wrap it with util.promisify(), or use a different library.

Explaining Promises and async/await is beyond the scope of this document. There are some excellent articles out there for this purpose. Ask your fellow developers if you get stuck or don’t understand something.

(TODO add some links to those excellent articles)

See an example - rewrite async.waterfall to async/await

Destructuring

Use destructuring to avoid repetition.

Destructuring objects

// before
var foo = {
  bar: bar,
};

// now
const foo = {
  bar,
};

// before
var foo = bar.foo;

// after
const { foo } = bar;

Destructuring arrays

// before
var x = [0, 1];
var a = x[0];
var b = x[1];

// after
let x = [0, 1];
let [a, b] = x;

Import

To load modules in Node.js use require as before. In React you can use import.

var => let, const

Don’t use var. If you’re going to change a variable, declare it with let. Use const everywhere else.

Note: var have function scope, but let and const have block scope.

Spread syntax (…)

Spread syntax and Rest parameters can make your life easier.

Arrow functions () => ()

Use arrow functions.

Tests

Mocha likes Promises. They simplify things a lot.