I tried importing nanoid into NestJS and got this error:
Error [ERR_REQUIRE_ESM]: require() of ES Module ....
... Instead change the require of index.js in ... to a dynamic import() which is available in all CommonJS modules.
The import code was:
import { Injectable } from '@nestjs/common';
import { nanoid } from 'nanoid'; //wont import, gives error
import { User } from './data-objects/user.object';
I also tried variation of the import statement:
// I tried this alternative
import * as nanoid from 'nanoid';
// Also tried this
const nanoid = require ( 'nanoid' );
Nothing worked.
Why is this error happening and how am I supposed to install nanoid in NestJS ?