[FIXED] How to call two functions with same name?

Issue

I installed 2 NPM packages and they both have a random(); function
how do I specify from which npm package am i calling the random method

here’s my code:


import { random } from 'superheroes';
import { random } from 'supervillains';

console.log(random());
console.log(random());

Solution

You can use import alias

import { random as randomSuperheroes } from 'superheroes';
import { random as randomSupervillains } from 'supervillains';

// You can call it using the alias name

console.log(randomSuperheroes()); // superheroes
console.log(randomSupervillains()); // supervillains

Answered By – kennarddh

Answer Checked By – Terry (Easybugfix Volunteer)

Leave a Reply

(*) Required, Your email will not be published