Mithril v2.2.8

Installation

CDN and online playground

If you're new to JavaScript or just want a very simple setup to get your feet wet, you can get Mithril.js from a CDN:

<script src="https://unpkg.com/mithril/mithril.js"></script>

If you would like to try out Mithril.js without setting up a local environment, you can easily use an online playground at flems.io/mithril.


npm

$ npm install mithril

TypeScript type definitions are available from DefinitelyTyped. They can be installed with:

$ npm install @types/mithril --save-dev

Create a project locally

You can use one of several existing Mithril.js starter templates such as

For example, if you'd like to get started with mithril-esbuild-starter, run the following commands:

# Clone the the template to a directory of your choice
npx degit kevinfiol/mithril-esbuild-starter hello-world

# Navigate to newly scaffolded project
cd ./hello-world/

# Install dependencies
npm install

# Build the app and watch for changes
npm run dev

Quick start with esbuild

esbuild documentation can be found here.

  1. Initialize the directory as an npm package.
$ npm init --yes
  1. Install required tools.
$ npm install mithril
$ npm install esbuild --save-dev
  1. Add a "start" entry to the scripts section in package.json.

    {
        "...": "...",
        "scripts": {
            "start": "esbuild index.js --bundle --outfile=bin/main.js --watch"
        }
    }
    

    Optionally, if you'd like to use JSX, you can use the --jsx-factory and --jsx-fragment flags with esbuild.

    {
        "...": "...",
        "scripts": {
            "start": "esbuild index.js --bundle --outfile=bin/main.js --jsx-factory=m --jsx-fragment='\"[\"' --watch"
        }
    }
    
  2. Create index.js file.

import m from "mithril";
m.render(document.getElementById("app"), "hello world");
  1. Create index.html file.
<!DOCTYPE html>
<body>
	<div id="app"></div>
	<script src="bin/main.js"></script>
</body>
  1. Run your bundler script.
$ npm run start
  1. Open index.html in a browser. You should see hello world rendered on your page.