Moving from Node Require Imports to Typescript Imports | Task

Ole Ersoy
Feb - 14  -  1 min

Scenario

We currently require fs like this in node:

const fs = require('fs');

We are wondering what the equivalent Typescript version is?

Approach

With Typescript, after running:

npm install --save-dev @types/node 

We can import the specific function we want from the fs library with a destructured import like this:

import { writeFileSync } from ‘fs';

Since we are now only importing what we need, the remaining parts of the library can be shaved off by tools like Webpack and Rollup.