ā Solution Below
Hello everyone , I hope that your doing well.
Recently, I tried to use an npm packages who is only ESM called **syllable .**
<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>
yarn add syllable// 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;
}
}
yarn start:dev
// 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);
}