How to use Superheroes Module in Node.js ?
Last Updated :
10 May, 2023
‘Superheroes‘ is a name of a module on NPM that generates random superheroes names. We can use this module in games, etc. There are millions of NPM module like this, which we can use in our projects. To know more about this module, you can search for this module on the website of NPM.
Installation of ‘superheroes’ module: To install ‘superheroes’ module into our project, we have to use the given instruction:
$ npm install superheroes
This instruction will add this module to our project and we can see the details in the package.json file in our project folder. Installed external modules are in node_modules and their all details are in JSON file ‘package.json’.
superheroes
Folder Structure:
Folder structure
Use the module in the project: Now, open the JavaScript code file and after this, we have to incorporate ‘superheroes’ module into our project. For this, we use require function. In this javascript code, require command is used firstly to incorporate the module into our project. Here, a function random() is used which generates random names of superheroes and console.log is used to show the output in the console.
Javascript
const superheroes = require( "superheroes" );
const superheroesNames = superheroes.random();
console.log(superheroesNames);
|
Run the Code: After completing our code, go to the terminal and run the JavaScript code file using this command. And this will generate the random superheroes names every time you run it, which we can see in the console. Run it, again and again, to generate more names randomly.
$ node index.js
Output:
output
Note: At the end, Press Ctrl + C to stop the npm.
Share your thoughts in the comments
Please Login to comment...