Copilot Shorts: Imports

bobbyg603 (Updated: October 4, 2023)
Copilot Shorts: Imports

Overview

This tutorial demonstrates how to leverage GitHub Copilot Chat to convert Node.js require statements into modern ES6 import syntax.

Requirements

  1. Visual Studio Code Insider’s build
  2. Copilot Chat extension
  3. Copilot Nightly extension

The Process

  1. Select the code block containing require statements
  2. Press Command+I to open inline chat
  3. Request conversion: “Convert these requires to import statements”
  4. Copilot generates the updated code

Pro tip: Press Command+I to bring up inline chat for quick code transformations.

Before

const express = require('express');
const path = require('path');
const fs = require('fs');

After

import express from 'express';
import path from 'path';
import fs from 'fs';

Conclusion

The combination of VS Code Insider’s build with Copilot Chat and Copilot Nightly enables TypeScript developers to efficiently modernize their codebase by replacing CommonJS require patterns with ES6 imports.

What legacy code patterns do you use Copilot to fix? Share below!