Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including electron-serve with all npm packages installed. Try it out:

var electronServe = require("electron-serve")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

electron-serve v1.0.0

Static file serving for Electron apps

electron-serve Build Status

Static file serving for Electron apps

Normally you would just use win.loadURL('file://…'), but that doesn't work when you're making a single-page web app, which most Electron apps are today, as history.pushState()'ed URLs don't exist on disk. It serves files if they exist, and falls back to index.html if not, which means you can use router modules like react-router, vue-router, etc.

Install

$ npm install electron-serve

Requires Electron 8 or later.

Usage

const {app, BrowserWindow} = require('electron');
const serve = require('electron-serve');

const loadURL = serve({directory: 'renderer'});

let mainWindow;

(async () => {
    await app.whenReady();

    mainWindow = new BrowserWindow();

    await loadURL(mainWindow);

    // The above is equivalent to this:
    await mainWindow.loadURL('app://-');
    // The `-` is just the required hostname
})();

API

serve(options)

options

Type: object

directory

Required
Type: string

The directory to serve, relative to the app root directory.

scheme

Type: string
Default: 'app'

Custom scheme. For example, foo results in your directory being available at foo://-.

partition

Type: string
Default: electron.session.defaultSession

The partition the protocol should be installed to, if you're not using Electron's default partition.

Related

Metadata

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free