⇒ Solution Below

Hello everyone , I hope that your doing well.

Recently, I tried to use an npm packages who is only ESM called **syllable .**

TLDR :

<aside> šŸ’” I’m lost with theses black box errors. Simply, How can I use only ESM libraries inside a nest.js project ? How can I use both only ESM or only Common.js project properly inside a production Nest.js project ?

</aside>

Steps to reproduce the problem

  1. mkdir test-project
  2. cd test-project
  3. nest new . (with yarn)
  4. code . (launch with vscode)
  5. install syllable with yarn ⇒ yarn add syllable
  6. add controller and service inside Appservice to test the library :
// app.controller.ts
import { Body, Controller, Get, Post } from '@nestjs/common';
import { AppService } from './app.service';

@Controller()
export class AppController {
  constructor(private readonly appService: AppService) {}

  @Get()
  getHello(): string {
    return this.appService.getHello();
  }

  @Post('demo')
  testSyllable(@Body('word') word: string) {
    return this.appService.testSyllable(word);
  }
}
// app.service.ts
import { Injectable } from '@nestjs/common';
import { syllable } from 'syllable';

@Injectable()
export class AppService {
  getHello(): string {
    return 'Hello World!';
  }

  testSyllable(word: string) {
    const result = syllable(word);
    return result;
  }
}
  1. run the project ⇒ yarn start:dev
  2. Get this error :

Untitled

  1. Try to use await import() like this :
// app.service.ts
async testSyllable(word: string) {
    const { syllable } = await import('syllable');
    const result = syllable(word);
    return result;
  }

// app.controller.ts
@Post('demo')
  async testSyllable(@Body('word') word: string) {
    return await this.appService.testSyllable(word);
  }
  1. The app start correctly, but when fetch the demo route, get this error :