npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

compose2spec

v1.0.0

Published

Convert Docker Compose files to Docker Engine API ServiceSpec objects

Readme

compose2spec

Convert Docker Compose files to Docker Engine API ServiceSpec objects for use with Docker Swarm.

Overview

compose2spec is a TypeScript library that converts Docker Compose YAML files (v2 and v3) into an array of Docker Engine API ServiceSpec objects. These objects can be directly used with dockerode.createService() to deploy services to a Docker Swarm cluster.

Features

  • ✅ Converts Docker Compose v2/v3 to Docker Swarm ServiceSpec
  • ✅ Full TypeScript support with type definitions
  • ✅ Supports environment variables, ports, volumes, networks
  • ✅ Handles resource limits (CPU, memory)
  • ✅ Supports deploy configurations (replicas, resources)
  • ✅ ESM and CommonJS module support
  • ✅ Comprehensive test coverage

Installation

npm install compose2spec

Usage

Basic Usage

import { parseComposeToServiceSpecs } from 'compose2spec';
import Dockerode from 'dockerode';

// Create Dockerode instance
const docker = new Dockerode();

// Parse compose file
const specs = await parseComposeToServiceSpecs('./docker-compose.yml');

// Create services in Docker Swarm
await Promise.all(specs.map(spec => docker.createService(spec)));

Using with string input

import { parseComposeToServiceSpecs } from 'compose2spec';

const composeYaml = `
version: "3.9"
services:
  web:
    image: nginx:latest
    ports:
      - "8080:80"
    environment:
      - FOO=bar
    deploy:
      replicas: 2
      resources:
        limits:
          memory: 512M
          cpus: "0.5"
`;

const specs = await parseComposeToServiceSpecs(composeYaml);

Synchronous parsing

import { parseComposeToServiceSpecsSync } from 'compose2spec';

const specs = parseComposeToServiceSpecsSync('./docker-compose.yml');

Supported Compose Features

  • Services - image, command, entrypoint, environment
  • Ports - published/target port mappings
  • Volumes - bind, volume, and tmpfs mounts
  • Networks - service network attachments
  • Deploy - replicas, resource limits (CPU/memory)
  • Environment - both array and object formats

API

parseComposeToServiceSpecs(input: string | object): Promise<ServiceSpec[]>

Async function to parse Docker Compose file to ServiceSpec array.

  • input: Path to compose file, YAML string, or parsed object

parseComposeToServiceSpecsSync(input: string | object): ServiceSpec[]

Synchronous version of the parser.

composeServiceToSpec(serviceName: string, serviceDef: object): ServiceSpec

Convert a single service definition to a ServiceSpec.

Example

Input (docker-compose.yml):

version: "3.9"
services:
  web:
    image: nginx:latest
    ports:
      - "8080:80"
    environment:
      - FOO=bar
      - BAZ=qux
    deploy:
      replicas: 2
      resources:
        limits:
          memory: 512M
          cpus: "0.5"
    volumes:
      - data:/usr/share/nginx/html
    networks:
      - frontend

networks:
  frontend:
volumes:
  data:

Output:

[
  {
    "Name": "web",
    "TaskTemplate": {
      "ContainerSpec": {
        "Image": "nginx:latest",
        "Env": ["FOO=bar", "BAZ=qux"],
        "Mounts": [{
          "Type": "volume",
          "Source": "data",
          "Target": "/usr/share/nginx/html"
        }]
      },
      "Resources": {
        "Limits": {
          "NanoCPUs": 500000000,
          "MemoryBytes": 536870912
        }
      }
    },
    "Mode": {
      "Replicated": {
        "Replicas": 2
      }
    },
    "Networks": [{
      "Target": "frontend"
    }],
    "EndpointSpec": {
      "Ports": [{
        "Protocol": "tcp",
        "PublishedPort": 8080,
        "TargetPort": 80
      }]
    }
  }
]

License

MIT