← Back to MithrilJS/mithril.js

How to Deploy & Use MithrilJS/mithril.js

Mithril.js Deployment and Usage Guide

Prerequisites

  • Node.js (version 12 or higher) - Required for npm package management
  • npm (version 6 or higher) - Comes with Node.js installation
  • Modern web browser - Mithril.js supports Firefox ESR, and the last two versions of Firefox, Edge, Safari, and Chrome

Installation

Using npm (Recommended)

npm install mithril --save

Using CDN (For quick prototyping)

Add one of these script tags to your HTML file:

<!-- Development -->
<script src="https://unpkg.com/mithril/mithril.js"></script>
<script src="https://cdn.jsdelivr.net/npm/mithril/mithril.js"></script>

<!-- Production -->
<script src="https://unpkg.com/mithril/mithril.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/mithril/mithril.min.js"></script>

TypeScript Support

npm install @types/mithril --save-dev

Configuration

Environment Setup

No specific environment variables are required for Mithril.js itself. However, you may need to configure:

  • Build tools (Webpack, Rollup, etc.) if using a bundler
  • Development server configuration for local development
  • API endpoints for your application's backend services

Project Structure

A typical Mithril.js project structure:

my-app/
├── src/
│   ├── components/
│   ├── views/
│   └── main.js
├── index.html
├── package.json
└── webpack.config.js (if using Webpack)

Build & Run

Development Setup

  1. Initialize your project:
npm init -y
npm install mithril --save
  1. Create a simple application (example from the documentation):
// src/main.js
import m from 'mithril'

const Hello = {
    view: function() {
        return m("main", [
            m("h1", "My first app"),
            m("button", {onclick: () => alert("hello!")}, "Say hello")
        ])
    }
}

m.mount(document.body, Hello)
  1. Create HTML file:
<!-- index.html -->
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>My App</title>
</head>
<body>
    <script src="src/main.js"></script>
</body>
</html>
  1. Run locally (using a simple HTTP server):
npx serve .

Production Build

If using a bundler like Webpack:

// webpack.config.js
const path = require('path')

module.exports = {
    entry: './src/main.js',
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: 'bundle.js'
    }
}
npx webpack --mode=production

Deployment

Static Hosting (Recommended)

Mithril.js applications are static and can be deployed to any static hosting service:

  • Vercel (formerly Zeit Now)
  • Netlify
  • GitHub Pages
  • AWS S3 + CloudFront
  • Firebase Hosting

Deployment Steps (Vercel example)

  1. Install Vercel CLI:
npm install -g vercel
  1. Deploy:
vercel --prod

Server-Side Rendering (Optional)

For SSR, you'll need a Node.js server:

npm install express --save
// server.js
const express = require('express')
const app = express()
const path = require('path')

app.use(express.static('dist'))

app.get('*', (req, res) => {
    res.sendFile(path.join(__dirname, 'dist', 'index.html'))
})

app.listen(3000, () => console.log('Server running on port 3000'))

Troubleshooting

Common Issues

  1. "m is not defined" error

    • Cause: Mithril not properly imported
    • Solution: Ensure you're importing Mithril correctly:
      import m from 'mithril'
      // or
      const m = require('mithril')
      
  2. Components not rendering

    • Cause: Incorrect mounting or missing return statement
    • Solution: Verify your component structure:
      const Component = {
          view: function() {
              return m("div", "Content")
          }
      }
      m.mount(document.body, Component)
      
  3. Routing issues

    • Cause: Incorrect route configuration
    • Solution: Check your route setup:
      m.route(document.body, "/", {
          "/": Home,
          "/about": About
      })
      
  4. Production build errors

    • Cause: Bundler configuration issues
    • Solution: Ensure your bundler is configured to handle ES6 modules and Mithril's dependencies

Performance Tips

  • Use m.mount() for single root components
  • Implement onbeforeremove for cleanup in components
  • Use m.request() for AJAX calls with proper error handling
  • Consider using m.fragment() for conditional rendering without wrapper elements

Getting Help

Contributing