Favicons

Favicons generator for Node.js

README

Favicons

NPM version Build Status

A Node.js module for generating favicons and their associated files. Originally built for Google's Web Starter Kit and Catalyst. Installed through NPM with:

  1. ```
  2. npm install favicons
  3. ```

Usage


Node.js


To use Favicons, require the appropriate module and call it, optionally specifying configuration and callback objects. A sample is shown on the right. The full list of options can be found on GitHub.

Favicons generates its icons locally using pure Javascript with no external dependencies.

Please note: Favicons is tested on Node 14 and above.

  1. ```js
  2. import { favicons } from "favicons";

  3. const source = "test/logo.png"; // Source image(s). `string`, `buffer` or array of `string`

  4. const configuration = {
  5.   path: "/", // Path for overriding default icons path. `string`
  6.   appName: null, // Your application's name. `string`
  7.   appShortName: null, // Your application's short_name. `string`. Optional. If not set, appName will be used
  8.   appDescription: null, // Your application's description. `string`
  9.   developerName: null, // Your (or your developer's) name. `string`
  10.   developerURL: null, // Your (or your developer's) URL. `string`
  11.   dir: "auto", // Primary text direction for name, short_name, and description
  12.   lang: "en-US", // Primary language for name and short_name
  13.   background: "#fff", // Background colour for flattened icons. `string`
  14.   theme_color: "#fff", // Theme color user for example in Android's task switcher. `string`
  15.   appleStatusBarStyle: "black-translucent", // Style for Apple status bar: "black-translucent", "default", "black". `string`
  16.   display: "standalone", // Preferred display mode: "fullscreen", "standalone", "minimal-ui" or "browser". `string`
  17.   orientation: "any", // Default orientation: "any", "natural", "portrait" or "landscape". `string`
  18.   scope: "/", // set of URLs that the browser considers within your app
  19.   start_url: "/?homescreen=1", // Start URL when launching the application from a device. `string`
  20.   preferRelatedApplications: false, // Should the browser prompt the user to install the native companion app. `boolean`
  21.   relatedApplications: undefined, // Information about the native companion apps. This will only be used if `preferRelatedApplications` is `true`. `Array<{ id: string, url: string, platform: string }>`
  22.   version: "1.0", // Your application's version string. `string`
  23.   pixel_art: false, // Keeps pixels "sharp" when scaling up, for pixel art.  Only supported in offline mode.
  24.   loadManifestWithCredentials: false, // Browsers don't send cookies when fetching a manifest, enable this to fix that. `boolean`
  25.   manifestMaskable: false, // Maskable source image(s) for manifest.json. "true" to use default source. More information at https://web.dev/maskable-icon/. `boolean`, `string`, `buffer` or array of `string`
  26.   icons: {
  27.     // Platform Options:
  28.     // - offset - offset in percentage
  29.     // - background:
  30.     //   * false - use default
  31.     //   * true - force use default, e.g. set background for Android icons
  32.     //   * color - set background for the specified icons
  33.     //
  34.     android: true, // Create Android homescreen icon. `boolean` or `{ offset, background }` or an array of sources
  35.     appleIcon: true, // Create Apple touch icons. `boolean` or `{ offset, background }` or an array of sources
  36.     appleStartup: true, // Create Apple startup images. `boolean` or `{ offset, background }` or an array of sources
  37.     favicons: true, // Create regular favicons. `boolean` or `{ offset, background }` or an array of sources
  38.     windows: true, // Create Windows 8 tile icons. `boolean` or `{ offset, background }` or an array of sources
  39.     yandex: true, // Create Yandex browser icon. `boolean` or `{ offset, background }` or an array of sources
  40.   },
  41.   shortcuts: [
  42.     // Your applications's Shortcuts (see: https://developer.mozilla.org/docs/Web/Manifest/shortcuts)
  43.     // Array of shortcut objects:
  44.     {
  45.       name: "View your Inbox", // The name of the shortcut. `string`
  46.       short_name: "inbox", // optionally, falls back to name. `string`
  47.       description: "View your inbox messages", // optionally, not used in any implemention yet. `string`
  48.       url: "/inbox", // The URL this shortcut should lead to. `string`
  49.       icon: "test/inbox_shortcut.png", // source image(s) for that shortcut. `string`, `buffer` or array of `string`
  50.     },
  51.     // more shortcuts objects
  52.   ],
  53. };

  54. try {
  55.   const response = await favicons(source, configuration);

  56.   console.log(response.images); // Array of { name: string, contents: }
  57.   console.log(response.files); // Array of { name: string, contents: }
  58.   console.log(response.html); // Array of strings (html elements)
  59. } catch (error) {
  60.   console.log(error.message); // Error description e.g. "An unknown error has occurred"
  61. }
  62. ```

The default sources are as follow (groupped by platform):

  1. ```javascript
  2. {
  3.   "android": [
  4.     "android-chrome-144x144.png",
  5.     "android-chrome-192x192.png",
  6.     "android-chrome-256x256.png",
  7.     "android-chrome-36x36.png",
  8.     "android-chrome-384x384.png",
  9.     "android-chrome-48x48.png",
  10.     "android-chrome-512x512.png",
  11.     "android-chrome-72x72.png",
  12.     "android-chrome-96x96.png"
  13.   ],
  14.   "appleIcon": [
  15.     "apple-touch-icon-1024x1024.png",
  16.     "apple-touch-icon-114x114.png",
  17.     "apple-touch-icon-120x120.png",
  18.     "apple-touch-icon-144x144.png",
  19.     "apple-touch-icon-152x152.png",
  20.     "apple-touch-icon-167x167.png",
  21.     "apple-touch-icon-180x180.png",
  22.     "apple-touch-icon-57x57.png",
  23.     "apple-touch-icon-60x60.png",
  24.     "apple-touch-icon-72x72.png",
  25.     "apple-touch-icon-76x76.png",
  26.     "apple-touch-icon-precomposed.png",
  27.     "apple-touch-icon.png"
  28.   ],
  29.   "appleStartup": [
  30.     "apple-touch-startup-image-1125x2436.png",
  31.     "apple-touch-startup-image-1136x640.png",
  32.     "apple-touch-startup-image-1242x2208.png",
  33.     "apple-touch-startup-image-1242x2688.png",
  34.     "apple-touch-startup-image-1334x750.png",
  35.     "apple-touch-startup-image-1536x2048.png",
  36.     "apple-touch-startup-image-1620x2160.png",
  37.     "apple-touch-startup-image-1668x2224.png",
  38.     "apple-touch-startup-image-1668x2388.png",
  39.     "apple-touch-startup-image-1792x828.png",
  40.     "apple-touch-startup-image-2048x1536.png",
  41.     "apple-touch-startup-image-2048x2732.png",
  42.     "apple-touch-startup-image-2160x1620.png",
  43.     "apple-touch-startup-image-2208x1242.png",
  44.     "apple-touch-startup-image-2224x1668.png",
  45.     "apple-touch-startup-image-2388x1668.png",
  46.     "apple-touch-startup-image-2436x1125.png",
  47.     "apple-touch-startup-image-2688x1242.png",
  48.     "apple-touch-startup-image-2732x2048.png",
  49.     "apple-touch-startup-image-640x1136.png",
  50.     "apple-touch-startup-image-750x1334.png",
  51.     "apple-touch-startup-image-828x1792.png"
  52.   ],
  53.   "favicons": [
  54.     "favicon-16x16.png",
  55.     "favicon-32x32.png",
  56.     "favicon-48x48.png",
  57.     "favicon.ico"
  58.   ],
  59.   "windows": [
  60.     "mstile-144x144.png",
  61.     "mstile-150x150.png",
  62.     "mstile-310x150.png",
  63.     "mstile-310x310.png",
  64.     "mstile-70x70.png"
  65.   ],
  66.   "yandex": [
  67.     "yandex-browser-50x50.png"
  68.   ]
  69. }

  70. ```

You can programmatically access Favicons configuration (icon filenames, HTML, manifest files, etc) with:

  1. ```js
  2. import { config } from "favicons";
  3. ```

Output


For the full list of files, check config/files.json. For the full HTML code, check config/html.js. Finally, for the full list of icons, check config/icons.json.

Questions


Why are you missing certain favicons?


Because pure Javascript modules aren't available at the moment. For example, the El Capitan SVG favicon and the Windows tile silhouette ability both require SVG support. If modules for these task begin to appear, please jump on the appropriate issue and we'll get on it ASAP.

Thank you


- @haydenbleasel for this great project.
- @phbernard for all the work we did together to make Favicons and RFG awesome.
- @addyosmani, @gauntface, @paulirish, @mathiasbynens and @pbakaus for their input on multiple source images.
- @sindresorhus for his help on documentation and parameter improvements.
- Everyone who opens an issue or submits a pull request to this repo :)

Contribution


Feel free to push your code if you agree with publishing under the MIT license.

When testing, don't forget to update snapshots whenever you edit them: npm test -- -u.