Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

node-notifier

mikaelbr14.8mMIT10.0.1TypeScript support: definitely-typed

A Node.js module for sending notifications on native Mac, Windows (post and pre 8) and Linux (or Growl as fallback)

notification center, mac os x 10.8, notify, terminal-notifier, notify-send, growl, windows 8 notification, toaster, notification

readme

node-notifier NPM version Install size Build Status

Send cross platform native notifications using Node.js. Notification Center for macOS, notify-osd/libnotify-bin for Linux, Toasters for Windows 8/10, or taskbar balloons for earlier Windows versions. Growl is used if none of these requirements are met. Works well with Electron.

macOS Screenshot Native Windows Screenshot

Input Example macOS Notification Center

Input Example

Actions Example Windows SnoreToast

Actions Example

Quick Usage

Show a native notification on macOS, Windows, Linux:

const notifier = require('node-notifier');
// String
notifier.notify('Message');

// Object
notifier.notify({
  title: 'My notification',
  message: 'Hello, there!'
});

Requirements

  • macOS: >= 10.8 for native notifications, or Growl if earlier.
  • Linux: notify-osd or libnotify-bin installed (Ubuntu should have this by default)
  • Windows: >= 8, or task bar balloons for Windows < 8. Growl as fallback. Growl takes precedence over Windows balloons.
  • General Fallback: Growl

See documentation and flow chart for reporter choice.

Install

npm install --save node-notifier

CLI

CLI has moved to separate project: https://github.com/mikaelbr/node-notifier-cli

Cross-Platform Advanced Usage

Standard usage, with cross-platform fallbacks as defined in the reporter flow chart. All of the options below will work in some way or another on most platforms.

const notifier = require('node-notifier');
const path = require('path');

notifier.notify(
  {
    title: 'My awesome title',
    message: 'Hello from node, Mr. User!',
    icon: path.join(__dirname, 'coulson.jpg'), // Absolute path (doesn't work on balloons)
    sound: true, // Only Notification Center or Windows Toasters
    wait: true // Wait with callback, until user action is taken against notification, does not apply to Windows Toasters as they always wait or notify-send as it does not support the wait option
  },
  function (err, response, metadata) {
    // Response is response from notification
    // Metadata contains activationType, activationAt, deliveredAt
  }
);

notifier.on('click', function (notifierObject, options, event) {
  // Triggers if `wait: true` and user clicks notification
});

notifier.on('timeout', function (notifierObject, options) {
  // Triggers if `wait: true` and notification closes
});

If you want super fine-grained control, you can customize each reporter individually, allowing you to tune specific options for different systems.

See below for documentation on each reporter.

Example:

const NotificationCenter = require('node-notifier/notifiers/notificationcenter');
new NotificationCenter(options).notify();

const NotifySend = require('node-notifier/notifiers/notifysend');
new NotifySend(options).notify();

const WindowsToaster = require('node-notifier/notifiers/toaster');
new WindowsToaster(options).notify();

const Growl = require('node-notifier/notifiers/growl');
new Growl(options).notify();

const WindowsBalloon = require('node-notifier/notifiers/balloon');
new WindowsBalloon(options).notify();

Or, if you are using several reporters (or you're lazy):

// NOTE: Technically, this takes longer to require
const nn = require('node-notifier');

new nn.NotificationCenter(options).notify();
new nn.NotifySend(options).notify();
new nn.WindowsToaster(options).notify(options);
new nn.WindowsBalloon(options).notify(options);
new nn.Growl(options).notify(options);

Contents

Usage: NotificationCenter

Same usage and parameter setup as terminal-notifier.

Native Notification Center requires macOS version 10.8 or higher. If you have an earlier version, Growl will be the fallback. If Growl isn't installed, an error will be returned in the callback.

Example

Because node-notifier wraps around terminal-notifier, you can do anything terminal-notifier can, just by passing properties to the notify method.

For example:

  • if terminal-notifier says -message, you can do {message: 'Foo'}
  • if terminal-notifier says -list ALL, you can do {list: 'ALL'}.

Notification is the primary focus of this module, so listing and activating do work, but they aren't documented.

All notification options with their defaults:

const NotificationCenter = require('node-notifier').NotificationCenter;

var notifier = new NotificationCenter({
  withFallback: false, // Use Growl Fallback if <= 10.8
  customPath: undefined // Relative/Absolute path to binary if you want to use your own fork of terminal-notifier
});

notifier.notify(
  {
    title: undefined,
    subtitle: undefined,
    message: undefined,
    sound: false, // Case Sensitive string for location of sound file, or use one of macOS' native sounds (see below)
    icon: 'Terminal Icon', // Absolute Path to Triggering Icon
    contentImage: undefined, // Absolute Path to Attached Image (Content Image)
    open: undefined, // URL to open on Click
    wait: false, // Wait for User Action against Notification or times out. Same as timeout = 5 seconds

    // New in latest version. See `example/macInput.js` for usage
    timeout: 5, // Takes precedence over wait if both are defined.
    closeLabel: undefined, // String. Label for cancel button
    actions: undefined, // String | Array<String>. Action label or list of labels in case of dropdown
    dropdownLabel: undefined, // String. Label to be used if multiple actions
    reply: false // Boolean. If notification should take input. Value passed as third argument in callback and event emitter.
  },
  function (error, response, metadata) {
    console.log(response, metadata);
  }
);

Note: The wait option is shorthand for timeout: 5. This just sets a timeout for 5 seconds. It does not make the notification sticky!

As of Version 6.0 there is a default timeout set of 10 to ensure that the application closes properly. In order to remove the timeout and have an instantly closing notification (does not support actions), set timeout to false. If you are using action it is recommended to set timeout to a high value to ensure the user has time to respond.

Exception: If reply is defined, it's recommended to set timeout to a either high value, or to nothing at all.


For macOS notifications: icon, contentImage, and all forms of reply/actions require macOS 10.9.

Sound can be one of these: Basso, Blow, Bottle, Frog, Funk, Glass, Hero, Morse, Ping, Pop, Purr, Sosumi, Submarine, Tink.

If sound is simply true, Bottle is used.


See Also:


Custom Path clarification

customPath takes a value of a relative or absolute path to the binary of your fork/custom version of terminal-notifier.

Example: ./vendor/mac.noindex/terminal-notifier.app/Contents/MacOS/terminal-notifier

Spotlight clarification

terminal-notifier.app resides in a mac.noindex folder to prevent Spotlight from indexing the app.

Usage: WindowsToaster

Note: There are some limitations for images in native Windows 8 notifications:

  • The image must be a PNG image
  • The image must be smaller than 1024×1024 px
  • The image must be less than 200kb
  • The image must be specified using an absolute path

These limitations are due to the Toast notification system. A good tip is to use something like path.join or path.delimiter to keep your paths cross-platform.

From mikaelbr/gulp-notify#90 (comment)

You can make it work by going to System > Notifications & Actions. The 'toast' app needs to have Banners enabled. (You can activate banners by clicking on the 'toast' app and setting the 'Show notification banners' to On)


Windows 10 Fall Creators Update (Version 1709) Note:

Snoretoast is used to get native Windows Toasts!

The default behaviour is to have the underlying toaster applicaton as appID. This works as expected, but shows SnoreToast as text in the notification.

With the Fall Creators Update, Notifications on Windows 10 will only work as expected if a valid appID is specified. Your appID must be exactly the same value that was registered during the installation of your app.

You can find the ID of your App by searching the registry for the appID you specified at installation of your app. For example: If you use the squirrel framework, your appID will be something like com.squirrel.your.app.

const WindowsToaster = require('node-notifier').WindowsToaster;

var notifier = new WindowsToaster({
  withFallback: false, // Fallback to Growl or Balloons?
  customPath: undefined // Relative/Absolute path if you want to use your fork of SnoreToast.exe
});

notifier.notify(
  {
    title: undefined, // String. Required
    message: undefined, // String. Required if remove is not defined
    icon: undefined, // String. Absolute path to Icon
    sound: false, // Bool | String (as defined by http://msdn.microsoft.com/en-us/library/windows/apps/hh761492.aspx)
    id: undefined, // Number. ID to use for closing notification.
    appID: undefined, // String. App.ID and app Name. Defaults to no value, causing SnoreToast text to be visible.
    remove: undefined, // Number. Refer to previously created notification to close.
    install: undefined // String (path, application, app id).  Creates a shortcut <path> in the start menu which point to the executable <application>, appID used for the notifications.
  },
  function (error, response) {
    console.log(response);
  }
);

Usage: Growl

const Growl = require('node-notifier').Growl;

var notifier = new Growl({
  name: 'Growl Name Used', // Defaults as 'Node'
  host: 'localhost',
  port: 23053
});

notifier.notify({
  title: 'Foo',
  message: 'Hello World',
  icon: fs.readFileSync(__dirname + '/coulson.jpg'),
  wait: false, // Wait for User Action against Notification

  // and other growl options like sticky etc.
  sticky: false,
  label: undefined,
  priority: undefined
});

See more information about using growly.

Usage: WindowsBalloon

For earlier versions of Windows, taskbar balloons are used (unless fallback is activated and Growl is running). The balloons notifier uses a great project called notifu.

const WindowsBalloon = require('node-notifier').WindowsBalloon;

var notifier = new WindowsBalloon({
  withFallback: false, // Try Windows Toast and Growl first?
  customPath: undefined // Relative/Absolute path if you want to use your fork of notifu
});

notifier.notify(
  {
    title: undefined,
    message: undefined,
    sound: false, // true | false.
    time: 5000, // How long to show balloon in ms
    wait: false, // Wait for User Action against Notification
    type: 'info' // The notification type : info | warn | error
  },
  function (error, response) {
    console.log(response);
  }
);

See full usage on the project homepage: notifu.

Usage: NotifySend

Note: notify-send doesn't support the wait flag.

const NotifySend = require('node-notifier').NotifySend;

var notifier = new NotifySend();

notifier.notify({
  title: 'Foo',
  message: 'Hello World',
  icon: __dirname + '/coulson.jpg',

  wait: false, // Defaults no expire time set. If true expire time of 5 seconds is used
  timeout: 10, // Alias for expire-time, time etc. Time before notify-send expires. Defaults to 10 seconds.

  // .. and other notify-send flags:
  'app-name': 'node-notifier',
  urgency: undefined,
  category: undefined,
  hint: undefined
});

See flags and options on the man page notify-send(1)

Thanks to OSS

node-notifier is made possible through Open Source Software. A very special thanks to all the modules node-notifier uses.

NPM downloads

Common Issues

Windows: SnoreToast text

See note on "Windows 10 Fall Creators Update" in Windows section. Short answer: update your appID.

Windows and WSL2

If you don't see notifications within WSL2, you might have to change permission of exe vendor files (snoreToast). See issue for more info

Use inside tmux session

When using node-notifier within a tmux session, it can cause a hang in the system. This can be solved by following the steps described in this comment

There’s even more info here https://github.com/mikaelbr/node-notifier/issues/61#issuecomment-163560801.

macOS: Custom icon without Terminal icon

Even if you define an icon in the configuration object for node-notifier, you will see a small Terminal icon in the notification (see the example at the top of this document).

This is the way notifications on macOS work. They always show the icon of the parent application initiating the notification. For node-notifier, terminal-notifier is the initiator, and it has the Terminal icon defined as its icon.

To define your custom icon, you need to fork terminal-notifier and build your custom version with your icon.

See Issue #71 for more info https://github.com/mikaelbr/node-notifier/issues/71.

Within Electron Packaging

If packaging your Electron app as an asar, you will find node-notifier will fail to load.

Due to the way asar works, you cannot execute a binary from within an asar. As a simple solution, when packaging the app into an asar please make sure you --unpack the vendor/ folder of node-notifier, so the module still has access to the notification binaries.

You can do so with the following command:

asar pack . app.asar --unpack "./node_modules/node-notifier/vendor/**"

Or if you use electron-builder without using asar directly, append build object to your package.json as below:

...
build: {
  asarUnpack: [
    './node_modules/node-notifier/**/*',
  ]
},
...

Using with pkg

For issues using with the pkg module. Check this issue out: https://github.com/mikaelbr/node-notifier/issues/220#issuecomment-425963752

Using Webpack

When using node-notifier inside of webpack, you must add the snippet below to your webpack.config.js.

This is necessary because node-notifier loads the notifiers from a binary, so it needs a relative file path. When webpack compiles the modules, it suppresses file directories, causing node-notifier to error on certain platforms.

To fix this, you can configure webpack to keep the relative file directories. Do so by append the following code to your webpack.config.js:

node: {
  __filename: true,
  __dirname: true
}

License

This package is licensed using the MIT License.

SnoreToast and Notifu have licenses in their vendored versions which do not match the MIT license, LGPL-3 and BSD 3-Clause to be specific. We are not lawyers, but have made our best efforts to conform to the terms in those licenses while releasing this package using the license we chose.

changelog

Changelog

v10.0.1

Fixes:

  • Fix custom path for windows #382

Thanks to @yoavain

v10.0.0

Breaking changes:

Setting NSAllowsArbitraryLoads as false for security reasons within terminal-notifier. Meaning non-https images/loads for terminal-notifier will no longer work. See #362

Fixes

  • fix: options.customPath doesn't work for windows toaster. See #373

v9.0.1

  • Fixes potential security issue with non-escaping input parameters for notify-send.

v9.0.0

Breaking changes:

  • Corrects mapping on snoretoast activate event. See #347.

Patches

  • Fix named pipe in WSL. See #342.
  • fixes possible injection issue for notify-send

v8.0.2

  • Fixes potential security issue with non-escaping input parameters for notify-send.

v8.0.0

Breaking changes:

  • Expire time for notify-send is made to match macOS and Windows with default time of 10 seconds. The API is changed to take seconds as input and converting it to milliseconds before passing it on to notify-send. See #341.

v7.0.2

  • Updates dependencies
  • Fixes issue with haning Windows notifications when disabled (#335)

v7.0.1

  • Fixes import of uuid, removes deprecation warnings

v7.0.0

Features

  • NotifySend support for app-name (#299, see docs)

Breaking Changes

  • All notify messages now have auto bound context to make it easier to pass as variables/arguments (#306)
  • Updated snoreToast to version 0.7.0 with new input features (#293)
  • Breaking snoreToast: Sanitizing data now changes "timedout" to "timeout"

v6.0.0

Breaking Changes

  • Dropped support for node v6. As of v6 we currently support node versions 8, 10, and 12 (latest).
  • Updated to the latest version of SnoreToast. This removes support for the wait option in that environment as it is now always on. Prepares the way for other new features added to the WindowsToaster.

Other

  • Update to latest version of dependencies.

v5.4.4

  • Fixes potential security issue with non-escaping input parameters for notify-send.

v5.4.3

  • Fixes potential security issue with non-escaping input parameters for notify-send.

v5.4.3

  • Reverts breaking dependency upgrades from v5.4.2 as some dependencies has removed Node 6 which is a breaking change.

v5.4.2

  • Updates dependencies

v5.4.1

  • Reverts changes to default timeout as they are causing some issues. See #271

v5.4.0

  • Prevent Spotlight from indexing terminal-notifier.app (#238)
  • Changes from legacy url.parse api
  • Adds default timeout to notification center
  • Adds mapping from timeout to expire time for linux
  • Enables the use of WindowsToaster when using WSL (#260)

v5.3.0

  • Re-adds notifu update.

v5.2.1

  • Rollback notifu update as it triggered Avast virus scan.

v5.2.0

  • Updates terminal-notifier dependency to v1.7.2, fixing memory leak. But not to v1.8.0 as this breaks how icons work.
  • Updates notifu with new subtitle "Notification"
  • Fix: issue with appID by removing default empty string (see README Windows section)
  • Fix: link notifier time property to notify-send expire-time flag

  • Minor change: use a more specific condition for enabling debug logging (#171)

v5.1.2

v5.0.2

Non-obligatory fail. Fixes issue with multiple actions for macOS.

v5.0.1

Obligatory fail. Fixes minor issue with non-JSON output for macOS.

v5.0.0

Breaking Changes

Note/TL;DR: If you are just using node-notifier with things like message, title and icon, v5 should work just as before.

  1. CLI is now removed. Can be found in separate project: https://github.com/mikaelbr/node-notifier-cli. This means you no longer get the notify bin when installing node-notifier. To get this do npm i [-g] node-notifier-cli
  2. Changed toaster implementation from toast.exe to Snoretoast. This means if you are using your custom fork, you need to change. SnoreToast has some better default implemented functionality.
  3. terminal-notifier dependency has been bumped to v1.7.1. With that there can be changes in the API, and supports now reply and buttons. Output has changed to JSON by default, this means the output of some functions of the terminal-notifier has broken. See https://github.com/julienXX/terminal-notifier for more details. See README for documentation on how to use the new features, or an example file.
  4. notify method will now throw error if second argument is something else than function (still optional): #138.

Additions

  1. Now supports *BSD systems: #142.
  2. With the new toaster implementation you can do more! For instance customize sound and close notification. See all options:
{
  title: void 0, // String. Required
  message: void 0, // String. Required if remove is not defined
  icon: void 0, // String. Absolute path to Icon
  sound: false, // Bool | String (as defined by http://msdn.microsoft.com/en-us/library/windows/apps/hh761492.aspx)
  wait: false, // Bool. Wait for User Action against Notification or times out
  id: void 0, // Number. ID to use for closing notification.
  appID: void 0, // String. App.ID. Don't create a shortcut but use the provided app id.
  remove: void 0, // Number. Refer to previously created notification to close.
  install: void 0 // String (path, application, app id).  Creates a shortcut <path> in the start menu which point to the executable <application>, appID used for the notifications.
}

Fixes

  1. Fixes new lines on messages on Windows: #123

Technical Changes

Internal changes for those who might be interested.

  1. Dependencies bumped
  2. Unnecessary dependencies removed (lodash.deepClone). Now uses JSON serialize/deserialize instead.
  3. Project is auto-formatted by prettier.
  4. Linting is added
  5. Added way to better debug what is happening by setting DEBUG env-var to true. See CONTRIBUTE.md for more details.

v4.6.1

  1. Adds npm ignore file, ignoring tests and examples from package.
  2. Fixes CI builds.

v4.6.0

  1. Adds support for Icon URL in Growl (by @gucong3000)
  2. Adds options for passing host and port to cli tool (reported by @el-davo)
  3. Fixes sanitize response on notify callback (by @MadLittleMods)
  4. Fixes use of new line in messages (by @gucong3000)
  5. Fixes use of file:///xxx protocol icon paths for Windows 8.1 (by @gucong3000)
  6. Fixes non-TTY usage and piping messages (reported by @simensen)
  7. Updates vendor terminal-notifier version to 1.6.3 (reported by @kid-icarus)

v4.5.0

Additions

  1. Adds syntactic sugar for notify. Now able to just pass message:
notifier.notify('My message');

See #45 for more info.

Fixes

  1. Improvements to docs and examples
  2. Updates semver dependency to support Webpacking with Electron.

v4.4.0

  1. Changes to exec terminal-notifier through execFile to allow for asar-packages
  2. Adds support for remote growl server
  3. Adds support for win7 with electron asar-package

v4.3.1

Obligatory patch fix:

  1. Adds new stdin CLI options to docs

v4.3.0

  1. Adds support for piping messages in to CLI. (With node-notifier installed as a CLI npm i -g node-notifier)
➜ echo "Message" | notify
➜ echo "Message" | notify -t "My Title"
➜ echo "Some message" | notify -t "My Title" -s

v4.2.3

  1. Fixed input arguments to CLI to be strings where they should be strings.

v4.2.2

  1. Fixed no notification when no message for the CLI. #58
  2. Changes which test to be sync, avoiding some edge cases with multiple notifications.

v4.2.1

  1. Minor fix for docs in CLI usage

v4.2.0

  1. Adds CLI support.
  2. Fixes Debug "HRESULT : 0xC00CE508" exception on Win8. PR #49

v4.1.2

  1. Fixes correct terminal-notifier (own fork https://github.com/mikaelbr/terminal-notifier) to support activate / click.

v4.1.1

  1. Fixes proper error codes for balloon: #42
  2. Removes unused debug files: #41
  3. Patches differences between subtitle for notify-send: #43
  4. Updates terminal-notifier dependency (removing black borders) #44 #18

v4.1.0

  1. Adds support for changing host and port for Growl.

v4.0.3

  1. Fixes Notification center issue with multiple callback events.
  2. Fixes error in source code: Fixes long-spaces to proper spaces

v4.0.2

  1. Fixes issue with immidiate notifu notifications (with wait : false)
  2. Fixes issue with boolean flags for notifu.
  3. Restructures directories. Making it easier to require notifiers directly.

v4.0.1

  1. Fixes issue with optional callback for notify-send

v4.0.0

Major changes and breaking API.

  1. require('node-notifier') now returns an instance with fallbackable notifications.
var notifier = require('node-notifier');
notifier.notify();
  1. Introduced a wait property (default false), to get user input for Notification Center, Windows Toaster, Windows Balloons and Growl. Sadly not for notify-send.
var notifier = require('node-notifier');
notifier.notify({ wait: true }, function (err, response) {
  // response is response after user have interacted
  // with the notification or the notification has timed out.
});
  1. All notification instances are now event emitters, emitting events click or timeout. This is only applicable if { wait: true }.
var notifier = require('node-notifier');
notifier.on('click', function (notificationObject, options) {
  // options.someArbitraryData === 'foo'
});
notifier.notify({ wait: true, someArbitraryData: 'foo' });
  1. WindowsToaster and NotificationCenter now can have sounds by doing { sound: true }. Default NotificationCenter sound is Bottle. Can still use define sound on Mac:
var notifier = require('node-notifier');
notifier.notify({ sound: true });
// For mac (same as sound: true on Windows 8)
notifier.notify({ sound: 'Morse' });

v3.4.0

  1. Adds Growl as priority over Balloons

v3.3.0

  1. Adds support for native Windows 7 and earlier (through task bar balloons)
  2. Changes growl implementation. Adds better support for GNTP

v3.2.1

  1. Fixes support for notifications from folders with spaces on Windows.

v3.2.0

  1. Adds native Windows 8 support.

v3.1.0

  1. Adds Growl as fallback for Mac OS X pre 10.8.

v3.0.6

  1. Fixes typo: Changes Growl app name from Gulp to Node.

v3.0.5

  1. Maps common options between the different notifiers. Allowing for common usage with different notifiers.

v3.0.4

  1. Fixes expires for notify-send (Issue #13)

v3.0.2

  1. Fixes version check for Mac OS X Yosemite

v3.0.0

  1. Updates terminal-notifier to version 1.6.0; adding support for appIcon and contentImage
  2. Removes parsing of output sent from notifier (Notification Center)