Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript compilation for module imports

I am working with an npm module (node-rsa), trying to use typescript. In the example for my module, I need to write some js like this (taken from the module docs).

var NodeRSA = require('node-rsa');
var key = new NodeRSA({b: 512});

However, when I type this in TypeScript:

import {NodeRSA} from 'node-rsa';
var key = new NodeRSA({b: 512});

it compiles to:

var node_rsa_1 = require('node-rsa');
var key = new node_rsa_1.NodeRSA({ b: 512 });

which throws the error:

"node_rsa_1.NodeRSA is not a function"

I am having to write my own .d.ts file, as there is nothing on DT, so I am not sure if that makes any difference?

like image 338
George Edwards Avatar asked May 29 '26 07:05

George Edwards


1 Answers

I tried this on my project and it worked:

import * as NodeRSA from 'node-rsa';

I think that's because node-rsa has a default export.

like image 197
Vini Avatar answered May 31 '26 21:05

Vini



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!