deploymentdockerfiledenofly

Deno unable to deploy on fly.io with Bcrypt


I am trying to deploy my Deno app to fly.io.

Everything works before deploying, but when I deploy I am getting this error:

Warning The following packages contained npm lifecycle scripts (preinstall/install/postinstall) that were not executed:
┠─ npm:bcrypt@5.1.1
┃
┠─ This may cause the packages to not work correctly.
┠─ Lifecycle scripts are only supported when using a `node_modules` directory.
┠─ Enable it in your deno config file:
┖─ "nodeModulesDir": "auto"
error: Uncaught (in promise) Error: Cannot find module '/deno-dir/npm/registry.npmjs.org/bcrypt/5.1.1/lib/binding/napi-v3/bcrypt_lib.node'
Require stack:
- /deno-dir/npm/registry.npmjs.org/bcrypt/5.1.1/bcrypt.js

I am aware the problem could stem from my Dockerfile or the fly.toml file but I am unsure of how to fix them.

Dockerfile:

# Based on https://github.com/denoland/deno_docker/blob/main/alpine.dockerfile

ARG DENO_VERSION=2.0.6
ARG BIN_IMAGE=denoland/deno:bin-${DENO_VERSION}
FROM ${BIN_IMAGE} AS bin

FROM frolvlad/alpine-glibc:alpine-3.13

RUN apk --no-cache add ca-certificates

RUN addgroup --gid 1000 deno \
  && adduser --uid 1000 --disabled-password deno --ingroup deno \
  && mkdir /deno-dir/ \
  && chown deno:deno /deno-dir/

ENV DENO_DIR /deno-dir/
ENV DENO_INSTALL_ROOT /usr/local

ARG DENO_VERSION
ENV DENO_VERSION=${DENO_VERSION}
COPY --from=bin /deno /bin/deno

WORKDIR /deno-dir
COPY . .

ENTRYPOINT ["/bin/deno"]
CMD ["run", "--allow-all", "main.ts"]

fly.toml:

app = "my-app"
primary_region = "nrt"

[build]

[env]
  PORT = "3001" # add this

  [processes]
  app = 'run --allow-all ./main.ts'

[http_service]
  internal_port = 3001
  force_https = true
  auto_stop_machines = true
  auto_start_machines = true
  min_machines_running = 0
  processes = ["app"]

[[vm]]
  cpu_kind = "shared"
  cpus = 1
  memory_mb = 1024

Solution

  • Incase anyone is struggling with this, Firstly, I changed the import to import * as bcrypt from "https://deno.land/x/bcrypt@v0.4.1/mod.ts"

    Then, I found a setup from the docker website and made some minor changes: https://hub.docker.com/r/denoland/deno

    FROM denoland/deno:2.0.6
    
    # The port that your application listens to.
    EXPOSE 1993
    
    WORKDIR /app
    
    # Create and set proper permissions for the working directory
    RUN mkdir -p /app && chown -R deno:deno /app
    
    # Switch to deno user
    USER deno
    
    # Copy dependency files first
    COPY --chown=deno:deno . .
    
    # Cache the dependencies
    RUN deno cache  main.ts
    
    # Run the application
    CMD ["run",  "--allow-net", "--allow-read", "--allow-env",  "main.ts"]