Emojibase
Emojibase is a comprehensive JavaScript library providing lightweight, up-to-date, and localized emoji JSON datasets, alongside utility functions and regex patterns for working with emoji characters. It is currently stable at version 17.0.0, which aligns with Emoji v17 and CLDR 48, reflecting its regular cadence of updates with new Unicode and CLDR releases. Key differentiators include its direct construction from official Unicode emoji data sources, adherence to Unicode Technical Standard #51, and localization capabilities derived from UTS #35, ensuring high accuracy and broad language support. The library aims to provide a robust and specification-compliant solution for emoji handling in various applications.
Common errors
-
SyntaxError: Cannot use import statement outside a module
cause Attempting to use ES module `import` syntax in a CommonJS (CJS) file or environment without proper transpilation or configuration.fixEnsure your `package.json` includes `"type": "module"` if you are writing an ESM project. If you must use CommonJS, use dynamic `import('emojibase')` or configure a bundler like Webpack/Rollup to handle ESM imports. -
Error: Emojibase requires Node.js v18.12.0 or higher.
cause Running Emojibase v16 or newer on an unsupported Node.js version (e.g., v16 or older).fixUpdate your Node.js installation to version 18.12.0 or newer to meet the minimum engine requirements for Emojibase. -
TypeError: Cannot read properties of undefined (reading 'find')
cause Trying to access methods on an undefined emoji dataset, potentially due to incorrect import paths or a failed data load.fixVerify that `getEmojiData` is correctly imported and awaited, and that the returned data is not `null` or `undefined`. Check for any network issues if loading data remotely, or ensure `emojibase-data` is properly installed if using local data files.
Warnings
- breaking Emojibase v17.0.0 updates its internal data to the latest Emoji v17 and CLDR 48 specifications. While mostly backwards compatible, changes in emoji properties or the introduction/removal of certain emojis might affect applications relying on specific data points or subset IDs.
- breaking Version 16.0.0 of Emojibase dropped support for Node.js v16. Projects must now run on Node.js v18.12.0 or higher.
- breaking Emojibase has transitioned to an ESM-first package, particularly since v16. While CJS might still function in some setups, direct `require()` calls for modern features or in pure ESM projects may fail.
Install
-
npm install emojibase -
yarn add emojibase -
pnpm add emojibase
Imports
- getEmojiData
const { getEmojiData } = require('emojibase');import { getEmojiData } from 'emojibase'; - getEmojiRegex
import { getEmojiRegex } from 'emojibase';import { getEmojiRegex } from 'emojibase-regex'; - Emoji
import type { Emoji } from 'emojibase';
Quickstart
import { getEmojiData } from 'emojibase';
import { getEmojiRegex } from 'emojibase-regex';
async function processEmojis() {
// Fetch all emoji data, defaulting to English
const allEmojis = await getEmojiData('en');
console.log(`Total emojis: ${allEmojis.length}`);
// Find a specific emoji by hex code (e.g., grinning face)
const grinningFace = allEmojis.find(emoji => emoji.hexcode === '1F600');
if (grinningFace) {
console.log(`Found emoji: ${grinningFace.emoji} - ${grinningFace.label}`);
}
// Get a regex that matches all known emojis
const emojiRegex = getEmojiRegex();
const text = "Hello world! 👋 This is an example with some emojis: 😂👍🌍.";
const matchedEmojis = text.match(emojiRegex);
if (matchedEmojis) {
console.log(`
Emojis found in text: ${matchedEmojis.join(', ')}`);
}
// You can also filter based on properties like 'skins'
const skinToneEmojis = allEmojis.filter(emoji => emoji.skins && emoji.skins.length > 0);
console.log(`
Emojis with skin tones: ${skinToneEmojis.slice(0, 5).map(e => e.emoji).join(', ')}...`);
}
processEmojis().catch(console.error);