Skip to content

healthcheck.js uses require() but package.json has "type": "module" — container stays unhealthy #1

@AlexandreCamillo

Description

@AlexandreCamillo

Bug

docker/healthcheck.js uses CommonJS require(), but package.json declares "type": "module", which makes all .js files ES modules. Node.js rejects the file at runtime:

ReferenceError: require is not defined in ES module scope, you can use import instead
This file is being treated as an ES module because it has a '.js' file extension
and '/app/package.json' contains "type": "module".

Impact

The Docker HEALTHCHECK fails on every check interval, so the container immediately enters the unhealthy state and never recovers. This has a critical downstream effect on Traefik v3: Traefik's Docker provider explicitly filters out unhealthy or starting containers and does not generate routes for them. The result is that the container runs fine internally but Traefik never routes traffic to it — 404 on every request from the outside even when traefik.enable=true is set.

Workaround (required to use with Traefik v3):

docker run --no-healthcheck ...

Steps to reproduce

  1. Run the image with default settings (no --no-healthcheck):
    docker run -d ghcr.io/alexandrecamillo/markup:latest
  2. After the startPeriod (10 s), check health:
    docker inspect markup --format '{{.State.Health.Status}}'
    # → unhealthy
    docker inspect markup --format '{{range .State.Health.Log}}{{.Output}}{{end}}'
    # → ReferenceError: require is not defined in ES module scope ...

Fix

Two options:

Option A — rename the file to use the .cjs extension so Node.js treats it as CommonJS:

docker/healthcheck.js → docker/healthcheck.cjs

Update the HEALTHCHECK instruction in Dockerfile accordingly.

Option B — rewrite healthcheck.js using ES module syntax:

import http from 'node:http';
// ... rest of the healthcheck logic

Either fix removes the need for --no-healthcheck and restores proper health reporting + Traefik compatibility.

Environment

  • Image: ghcr.io/alexandrecamillo/markup:latest (built 2026-05-09)
  • Node.js: v22.22.2
  • Traefik: v3.7.0 (Docker provider, network_mode: host)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions