Install Marko

Note

Experiment with Marko directly in the browser using the Playground with no local setup.

Prerequisites

Marko Run

Marko Run is the official application framework, powered by Vite. It is the recommended starting point for new projects.

npm init marko -- -t basic -n my-marko-app
cd my-marko-app
npm run dev

Available starter templates come from marko-js/examples:

TemplateDescription
basicMarko Run starter with file-based routing
appFull Marko Run application with testing and styling setup
libraryReusable component library with TypeScript and Storybook
vite-expressCustom Express server integrated with @marko/vite

Non-interactive environments (CI, piped stdin) accept defaults automatically. Pass flags to control scaffolding directly: -t for the template, -n / --name for the project directory, -y / --yes to accept defaults, --no-install to skip dependency installation, and --no-git to skip Git initialization.

Edit src/routes/+page.marko to modify the index page, and see File-based Routing to add routes.

Manual Setup

Projects with custom server or bundler architectures can configure Marko directly with @marko/vite or another supported bundler.

Vite with Express

  1. Initialize an ESM project:

    mkdir my-marko-app
    cd my-marko-app
    npm init -y
    npm pkg set type=module
  2. Install Marko, Express, and Vite:

    npm install marko express
    npm install -D vite @marko/vite
  3. Register the Marko plugin in vite.config.ts:

    import { defineConfig } from "vite";
    import marko from "@marko/vite";
    
    export default defineConfig({
      plugins: [marko()],
    });
  4. Create a template at src/routes/index.marko and a route module at src/routes/index.js:

    src/routes/index.marko
    <html lang="en">
      <body>
        <h1>Hello Marko!</h1>
      </body>
    </html>
    html lang="en"
      body
        h1 -- Hello Marko!
    src/routes/index.js
    import template from "./index.marko";
    
    export default function (_req, res) {
      res.header("Content-Type", "text/html");
      template.render().pipe(res);
    }

    The page template rendered by a server route module is treated as a page entry by @marko/vite, which automatically injects client assets when rendered.

  5. Create a development server in index.js:

    import express from "express";
    import { createServer } from "vite";
    
    const app = express();
    const vite = await createServer({
      server: { middlewareMode: true },
      appType: "custom",
    });
    
    app.use(vite.middlewares);
    app.get("/", async (req, res, next) => {
      const handler = (await vite.ssrLoadModule("./src/routes/index.js"))
        .default;
      handler(req, res, next);
    });
    
    app.listen(3000);

    For production builds and full server examples, see the vite-express example.

  6. Start the development server and open http://localhost:3000:

    node index.js

Browser-Only Setup

By default, @marko/vite links a server build with a browser build. For client-only applications without a server build, pass linked: false to marko() and mount the root template with Template.mount:

vite.config.ts
import { defineConfig } from "vite";
import marko from "@marko/vite";

export default defineConfig({
  plugins: [marko({ linked: false })],
});
index.html
<div id="app"></div>
<script type="module" src="/src/main.ts"></script>
src/main.ts
import App from "./app.marko";

App.mount({}, document.getElementById("app")!);
Note

With linked: false, @marko/vite cannot code-split lazy-loaded tags; with { load } imports compile as eager imports.

TypeScript Setup

Adding a tsconfig.json at the project root enables TypeScript inside .marko templates:

{
  "include": ["src/**/*"],
  "compilerOptions": {
    "allowSyntheticDefaultImports": true,
    "lib": ["dom", "ESNext"],
    "module": "ESNext",
    "moduleResolution": "bundler",
    "noEmit": true,
    "noImplicitOverride": true,
    "noUnusedLocals": true,
    "outDir": "dist",
    "resolveJsonModule": true,
    "skipLibCheck": true,
    "strict": true,
    "target": "ESNext",
    "types": ["vite/client"],
    "verbatimModuleSyntax": true
  }
}

For CLI and CI type checking, install @marko/type-check and run mtc instead of tsc:

npm install -D @marko/type-check
npx mtc

See the TypeScript reference for full type syntax and configuration details.

IDE Support

The Marko VS Code extension bundles syntax highlighting, formatting, TypeScript checking, and IntelliSense. Other editors can connect to the Marko Language Server.

Next Steps


Contributors

Helpful? You can thank these awesome people! You can also edit this doc if you see any issues or want to improve it.