«   Previous tip Next tip   »

VS Code: Easily convert require statements into modern ES module imports

Last updated: 10th March 2023

VS Code can convert your code that uses require()'s, like this:

const path = require("path");
const crypto = require("crypto");

const fetch = require("node-fetch");
const { name } = require("./animal.js");

Into this code that uses import's, like this:

import path from "path";
import crypto from "crypto";

import fetch from "node-fetch";
import { name } from "./animal.js";

This also works with exports!

To use this feature:

  1. With the cursor next to the first r in the first require statement, observe three dots appear underneath the r.
  2. Enter Cmd + Shift + P.
  3. And select Quick fix
  4. The option to convert to ESM appears, select that with Enter.

Alternatively, click the lighbulb 💡️ icon that appears.

«   Previous tip Next tip   »

Sign up to receive a developer tip, in the form of a gif, in your inbox each week