17

I'm trying to configure Babel for Node v6.9.2. I want to use async/await constructs.

Because I'm new to Babel and all Node infrastructure, I confused how to configure it properly:

  • What preset should I use? Node is already implemented most of the ES6 features. So I don't want Babel to transpile features already supported by Node 6.9.x (arrow functions, new import mechanism etc) for performance reasons.

  • What plugins should I include so I can use async/await? There I also confused, because after some researching I found several plugins: syntax-async-functions, transform-async-to-generator and some more.

Example of .babelrc will help.

Thanks

mark
  • 1,953
  • 1
  • 24
  • 47
WelcomeTo
  • 19,843
  • 53
  • 170
  • 286
  • 1
    *"already supported by Node 6.9.x (arrow functions, new import mechanism etc)"* Node 6.x doesn't support `import`/`export`. – T.J. Crowder Feb 17 '17 at 13:46
  • You should be good with `transform-async-to-generator` – Hosar Feb 17 '17 at 13:47
  • 1
    *"So I don't want Babel to transpile features already supported by Node 6.9.x...for performance reasons"* Other than startup performance (which is impacted), *runtime* performance will probably be better. The V8 team have been working diligently to make the performance of the new features performance-comparable to their previous analogs, but a lot of that work is newer than you'll find in the V8 in Node 6.x. For instance, it's only the latest release (5.7) that they think they've reached performance parity: https://v8project.blogspot.co.uk/2017/02/v8-release-57.html Just FWIW. – T.J. Crowder Feb 17 '17 at 13:48
  • @T.J.Crowder We found that native `class` constructors are faster than transpiled ones because of the "class call check", so when the transpiler emits complex code to match ES6 behavior it might actually be slower than the ES5 equivalent. – Bergi Feb 17 '17 at 15:26
  • @Bergi: Makes sense. Some of what Babel outputs is pretty...convoluted. :-) – T.J. Crowder Feb 17 '17 at 15:31

2 Answers2

15

What preset should I use?

You don't need to use any preset. Presets are just a collection of plugins which makes it easier to use if you want to transpile a set of features (for instance all ES2015 with preset-es2015). But when you want to transpile only a selection of these features, you only include the corresponding plugins.

What plugins should I include so I can use async/await?

Because Node 6 supports generators, you can use transform-async-to-generator with the following .babelrc:

{
  "plugins": ["transform-async-to-generator"]
}

And of course you would need to add plugins if you need to transpile more unsupported features.

Alternative babel-preset-env

babel-preset-env automatically determines what plugins you need for the specified environment. This will not include any plugins that are not necessary. To specify your current Node version you would use this .babelrc:

{
  "presets": [
    ["env", {
      "targets": {
        "node": "current"
      }
    }]
  ]
}
Michael Jungo
  • 31,583
  • 3
  • 91
  • 84
10

Short answer

Use Babel preset for Node 6.x:

Long answer

To see what ES feature is supported in a given Node version, see:

For async/await support in particular, see:

If you use Node v7.x (the current version) then you can use the --harmony flag and use async/await natively without transpilation.

Node v8.x (available as nightly builds) doesn't even need the --harmony flag for that.

But note that Node doesn't support import/export - to know why see:

Ryan Shillington
  • 23,006
  • 14
  • 93
  • 108
rsp
  • 107,747
  • 29
  • 201
  • 177