Reputation: 21
import { Injectable, OnModuleInit } from '@nestjs/common';
@Injectable()
export class PotatoService implements OnModuleInit {
constructor() {}
async onModuleInit(): Promise<void> {
const chalk = (await import('chalk')).default;
console.log(chalk.blue('potato service init'));
}
}
i try to use import(), but get a error
node:internal/process/promises:289
triggerUncaughtException(err, true /* fromPromise */);
^
Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/potato/Desktop/learn/nest-demo/node_modules/.pnpm/[email protected]/node_modules/chalk/source/index.js from /Users/potato/Desktop/learn/nest-demo/dist/bootstrap.js not supported.
Instead change the require of index.js in /Users/potato/Desktop/learn/nest-demo/dist/bootstrap.js to a dynamic import() which is available in all CommonJS modules.
at /Users/potato/Desktop/learn/nest-demo/dist/bootstrap.js:64:93
at async bootstrap (/Users/potato/Desktop/learn/nest-demo/dist/bootstrap.js:64:20) {
code: 'ERR_REQUIRE_ESM'
}
Node.js v20.10.0
so how to import a esmodule only package in nest ?
use import() not import xxx from xxx, but not work
Upvotes: 2
Views: 35
Reputation: 246
Chalk version 5, being a pure 'ESM' package, can't be directly used in your current CommonJS project. Here are your options:
--experimental-require-module
flag to import ESM in CommonJS, which is not ideal.Recommendation: Migrating to ESM is the best long-term approach for a more organized and modern project structure."
Upvotes: 0