'No Autocomplete for Modules Imported with Require

I have a simple file foo.ts that has these lines

/// <reference types="node" />

const fs = require('fs')

VSCode is unable to autocomplete the fs variable.

If the file were renamed to foo.js, then autocomplete works. If I changed the 2nd line to

import * as fs from 'fs'

Then autocomplete works too in the TS file too. However, now I can't run the file because it complains that import cannot be used outside a module.

I have seen solutions to fiddle with package.json to change the type to module, or tsconfig.json to change the module to commonJS. The latter didn't work for me, and I can't change the former.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source