I am trying to use the superheroes
npm package in my Node.js project to generate a random superhero name, but I keep encountering an error:
TypeError: superheroes.random is not a function
Here is my code:
import superheroes from "superheroes";
const name = superheroes.random();
console.log(`I am ${name}!`);
When I run it, I also get this warning:
(MODULE_TYPELESS_PACKAGE_JSON) Warning: Module type of file is not specified and it doesn't parse as CommonJS. Reparsing as ES module because module syntax was detected.
To eliminate this warning, add "type": "module" to package.json.
Steps I've tried:
Added "type": "module"
in package.json
to resolve the module type warning.
Reinstalled the superheroes
package using:
npm uninstall superheroes
npm install superheroes
Checked the Node.js version (node -v
) and ensured it's up-to-date.
Observations:
When I log the superheroes
object (console.log(superheroes);
), it doesn't seem to have a random()
function.
The superheroes.all
property works fine (it contains a list of superhero names).
Why is superheroes.random
not working as expected? Is there a bug in the package, or am I using it incorrectly? How can I generate a random superhero name using this package?
Back in version 3.0.0, you used to have a random()
method. It was changed in 4.0.0, and is now called randomSuperhero()
.
Either downgrade your installation to use 3.0.0, or, better yet, change your code to use the modern randomSuperhero()
.