AlertifyJS - Browser Dialogs & Notifications
AlertifyJS is a lightweight JavaScript framework designed for creating customizable and visually appealing browser dialogs (alerts, confirms, prompts) and unobtrusive notification messages. Currently stable at version 1.14.0, the package was last updated approximately two years ago, suggesting an inactive release cadence. It aims to replace native browser dialogs, which are often blocking and visually inconsistent, with a more modern and user-friendly experience. Key differentiators include its small footprint, out-of-the-box theme support (AlertifyJS, Semantic UI, Bootstrap), responsiveness across devices, and built-in internationalization (i18n) and Right-to-Left (RTL) layout support. Unlike full-fledged UI libraries, AlertifyJS focuses solely on dialogs and notifications, offering a simple API for common use cases.
Common errors
-
Uncaught ReferenceError: alertify is not defined
cause The `alertify` global object or module export was not loaded or imported correctly before being used.fixEnsure AlertifyJS is imported at the top of your script (`import alertify from 'alertifyjs';`) or that the script tag for the library is included and loaded before any `alertify` calls in a global context. -
Dialogs appear as unstyled, basic browser prompts/alerts.
cause The necessary CSS stylesheets (core and theme) for AlertifyJS have not been correctly imported or linked in the HTML.fixAdd the CSS imports: `import 'alertifyjs/build/css/alertify.min.css';` and `import 'alertifyjs/build/css/themes/default.min.css';` (or another theme) to your JavaScript entry point, or link them directly in your HTML `<head>` section. -
TypeError: Cannot read properties of undefined (reading 'alert')
cause Often occurs in module environments if `alertify` is imported but the default export is not correctly captured or the module resolves to `undefined`.fixVerify that `import alertify from 'alertifyjs';` is used for ESM or `const alertify = require('alertifyjs');` for CJS, ensuring the exported `alertify` object is correctly assigned and accessible.
Warnings
- breaking The license for AlertifyJS changed from MIT to GPLv3 starting with version 1.8.0. This is a significant change in licensing terms that affects how the software can be used, modified, and distributed, especially in commercial projects.
- breaking AlertifyJS is an 'extreme makeover' of the original `alertify.js` library by @fabien-d. This implies significant API changes and behavioral differences, meaning code written for `alertify.js` will likely not work directly with AlertifyJS without modification.
- gotcha If a callback is provided to a notification (e.g., `alertify.success('message', callback)`), clicking the notification message will not automatically close it. This differs from previous behavior in the original `alertify.js` and might lead to unexpected user experience if not accounted for.
- gotcha AlertifyJS supports the `prefers-reduced-motion` CSS media feature. If a user has enabled reduced motion in their operating system settings, all animation and transition effects within AlertifyJS will be automatically disabled to respect user preference.
- gotcha When using bundlers or module loaders, explicitly importing the necessary CSS files (core and theme) is crucial. Neglecting these imports will result in unstyled, basic browser dialogs and notifications, which do not align with the library's 'pretty dialogs' promise.
Install
-
npm install alertifyjs -
yarn add alertifyjs -
pnpm add alertifyjs
Imports
- alertify
const alertify = require('alertifyjs');import alertify from 'alertifyjs';
- CSS (Core)
import 'alertifyjs/alertify.min.css';
import 'alertifyjs/build/css/alertify.min.css';
- CSS (Theme)
import 'alertifyjs/themes/default.min.css';
import 'alertifyjs/build/css/themes/default.min.css';
Quickstart
import alertify from 'alertifyjs';
import 'alertifyjs/build/css/alertify.min.css';
import 'alertifyjs/build/css/themes/semantic.min.css'; // Or 'default.min.css', 'bootstrap.min.css'
// Basic Alert
alertify.alert('Hello World!', function(){ alertify.message('You clicked OK'); });
// Confirm Dialog
alertify.confirm('Confirm Action', 'Are you sure you want to proceed?',
function(){
alertify.success('You clicked OK');
},
function(){
alertify.error('You clicked Cancel');
}
);
// Prompt Dialog
alertify.prompt('Prompt Example', 'Please enter your name:', 'Anonymous',
function(evt, value){
alertify.success(`Hello ${value}!`);
},
function(){
alertify.error('You clicked Cancel');
}
);
// Notification Message
alertify.success('Success message here!');
alertify.error('Error message here!');
alertify.warning('Warning message here!');
alertify.message('Normal message here.');
console.log('AlertifyJS initialized and examples run.');