5

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?

Peter Baer
  • 1,232
  • 3
  • 14
  • 19

2 Answers2

4

Can you try Add allowSyntheticDefaultImports: true to your 

tsconfig.json

 seems like still an open issue in Typescript.

Krishnadas PC
  • 5,981
  • 2
  • 53
  • 54
0

Basically you need remove comment in tsconfig.json at line 46, because default ts config file have this option but it's commented by default

Jerzy Drożdż
  • 438
  • 6
  • 8