Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to import "crypto" system library in NodeJS because it is suddenly undefined?

I updated a NestJS based project from 2021 that contains an import like this:

import crypto from 'crypto';

Nowadays, with Node 18, crypto is undefined.

What happened to this lib, and is there a replacement available? According to NodeJS documentation, this actually should not happen to be removed.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You might try importing via node:crypto. The node: namespace is something new with ESM. If this isn’t an ESM project and just NestJS + Typescript, then you can try import * as crypto from 'crypto' or try setting syntheticDefaultImports: true in the tsconfig. Note, that setting is already set to true if esModuleInterop is set to true

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading