How to use ES6 import with 'request' npm module

Peter Baer picture Peter Baer · Dec 17, 2017 · Viewed 8.4k times · Source

In ES6-ifying some TypeScript code (the project I'm working runs in both the browser and a Node server, I'd like to tree-shake the browser bundle), I'm trying to eliminate uses of require and only use import. But when I do this...

import * as request from 'request';

and subsequently call request(), I get runtime errors in Node (after using babel to make the code ES5, and thus Node, compatible):

TypeError: request is not a function

On the other hand, if I do this:

import request from 'request';

then the TypeScript compiler complains with

error TS1192: Module '"<mypath>/node_modules/@types/request/index"' has no default export.

If I manually change the compiled JS code to use import request from 'request';, it actually works fine... how can I force the TS compiler to accept this code and just pass it through?

Answer

Krishnadas PC picture Krishnadas PC · Dec 17, 2017

Can you try Add allowSyntheticDefaultImports: true to your 

tsconfig.json

 seems like still an open issue in Typescript.