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.

>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

Leave a Reply