Moving from Node Require Imports to Typescript Imports

Ole Ersoy
1 min readJun 4, 2018

--

Photo by Fabio Santaniello Bruun on Unsplash

Scenario

We currently require fs like this in node:

const fs = require('fs');

We are wondering what the equivalent typescript version is?

Answer

With Typescript, after running npm install --save-dev @types/node we can import the fs the specific function we want from the fs library with a destructured import like this:

import { writeFileSync } from ‘fs';

The destructured part is important in the event that you want to minimize the module you are writing with tools like Webpack / Rollup. Since we are now only importing what we need, the remaining parts of the library we are importing from can be shaved off.

Looking for a more elaborate example. Checkout Using Papaparse With Typescript.

--

--

Ole Ersoy
Ole Ersoy

Written by Ole Ersoy

Founder of Firefly Semantics Corporation

Responses (1)