Issue with 'upper-case' module in Node.js: ERR_REQUIRE_ESM when using require()

625 views Asked by At

I am facing a problem with the 'upper-case' module in my Node.js application. I'm trying to use the upper-case module to convert a string to uppercase, but I'm encountering an error related to ESM and require().

Node.js Version:

Node.js v20.9.0

Code:

var http = require('http');
var uc = require('upper-case');

http.createServer((req, res) => {
    res.writeHead(200, { 'Content-Type': 'text/html' });
    res.write(uc.upperCase("hello world!"));
    res.end();
}).listen(5000);

console.log('Uppercase Server running...');

Error Message:

Error [ERR_REQUIRE_ESM]: require() of ES Module ... not supported.
Instead, change the require of index.js to a dynamic import().

I have tried using different syntax for require, but the issue persists. I have checked the 'upper-case' module's documentation and GitHub page for any known issues, but haven't found a solution.

1

There are 1 answers

1
Selaka Nanayakkara On

As per the error state itself it no longer uses require.

Error [ERR_REQUIRE_ESM]: require() of ES Module ... not supported. Instead, change the require of index.js to a dynamic import().

If you really need you should try to import insted, Try something like below :

import { upperCase } from "upper-case";

or else use javascript inbuilt functionality instead:

var str = "uppercase";
var res = str.toUpperCase();
console.log(res)