This npm package allows you to fetch flags of various countries and get information about them.
To use the package in your project, install it with the following command:
npm install country-flag-utils
In your File:
const flag = require('country-flag-utils') //Import the Package
To retrieve information about a flag, use the getFlag(countryIdentifier, language)
function. Example:
// Get flag and name of Germany
console.log(flag.getFlag("de"));
console.log(flag.getFlag("de", flag.Language.ENGLISH));
// Output:
{
name: 'Germany',
code: 'de',
url: 'https://www.welt-flaggen.de/data/flags/h160/de.webp'
}
To retrieve the URL of a flag, use the getFlagUrl(countryIdentifier)
function. Example:
// Get flag URL of Germany
console.log(flag.getFlagUrl("de"));
// Output:
https://www.welt-flaggen.de/data/flags/h160/de.webp
To fetch random information about a flag, use the getRandomFlag(language)
function. Example:
// Get a Random Flag
console.log(flag.getRandomFlag());
console.log(flag.getRandomFlag(flag.Language.ENGLISH));
// Output:
{
name: 'Germany',
code: 'de',
url: 'https://www.welt-flaggen.de/data/flags/h160/de.webp'
}
flag.Language.LANGUAGE
-
getFlag(countryIdentifier, language)
: Returns information about the flag and name of a country based on its identifier. Language is optional and defaults to English. -
getFlagUrl(countryIdentifier)
: Returns the URL of a country's flag based on its identifier. -
getRandomFlag(language)
: Returns random information about the flag and name of a country, optionally in a specific language.