One of the most epic releases lately. Top-Level Await and named imports from CJS modules are now supported in v14.3.0.
Like Deno, it’s now possible to use the await keyword outside of async functions.
Let’s take a look at an example.
We will fetch a random user from a API and say hello.
// hello.js
export const sayHello = (name) => `Hello, ${name}!`;
// index.js
import axios from "axios";
import { sayHello } from "./hello.js";
const { data } = await axios.get("https://api.namefake.com/");
console.log(sayHello(data.name));
Add the type
property in your package.json
file with value of module
.
{
"name": "node-14.3.0",
"version": "0.0.0",
"main": "index.js",
"type": "module",
"dependencies": {
"axios": "^0.20.0"
}
}
Run it with the --harmony-top-level-await
flag.
node --harmony-top-level-await index.js
Output:
Hello, Miss Mellie Mosciski!
When Node finally supports remote imports, then it's a nail in the coffin for Deno.
Cover photo by @cookiethepom on Unsplash