Unable to deploy basic typescript express api

Hey everyone, I’m building a basic express server that utilises typescript. At the moment, I have two basic endpoints. But will be setting up a postgres database once i’ve sorted out this deployment issue.

Whenever I try to run fly deploy, I get the following error:

Error: failed to fetch an image or build from source: error building: failed to solve: executor failed running [/bin/sh -c npm run build]: exit code: 1

Any idea on what could be causing this issue? Thanks

Here’s my dockerfile, fly toml and package.json files for reference:

Dockerfile:

# Adjust NODE_VERSION as desired
ARG NODE_VERSION=16.13.2
FROM node:${NODE_VERSION}-slim as base

LABEL fly_launch_runtime="Node.js"

# Node.js app lives here
WORKDIR /app

# Set production environment
ENV NODE_ENV=production


# Throw-away build stage to reduce the size of the final image
FROM base as build

# Install packages needed to build node modules and TypeScript
RUN apt-get update -qq && \
    apt-get install -y python pkg-config build-essential && \
    npm install -g typescript

# Install node modules
COPY package-lock.json package.json ./
RUN npm ci

# Copy application code
COPY . .

# Build TypeScript files
RUN npm run build


# Final stage for app image
FROM base

# Copy built application
COPY --from=build /app/build /app

# Start the server by default, this can be overwritten at runtime
EXPOSE 3000
CMD [ "node", "build/index.js" ]

Fly.toml

app = "ladybug"
primary_region = "lhr"

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

Package.json

{
    "name": "server",
    "version": "1.0.0",
    "description": "",
    "main": "build/index.js",
    "scripts": {
        "test": "jest --coverage",
        "build": "tsc",
        "dev": "ts-node-dev app.ts",
        "lint": "eslint --ext .ts .",
        "tidy": "npx prettier . --write",
        "start": "node build/index.js"
    },
    "author": "",
    "license": "ISC",
    "dependencies": {
        "dotenv": "^16.3.1",
        "express": "^4.18.2",
        "pg": "^8.11.1",
        "sequelize": "^6.32.1"
    },
    "devDependencies": {
        "@flydotio/dockerfile": "^0.3.3",
        "@types/express": "^4.17.17",
        "@types/jest": "^29.5.3",
        "@types/pg": "^8.10.2",
        "@types/sequelize": "^4.28.15",
        "@types/supertest": "^2.0.12",
        "@typescript-eslint/eslint-plugin": "^6.0.0",
        "@typescript-eslint/parser": "^6.0.0",
        "eslint": "^8.45.0",
        "eslint-config-prettier": "^8.8.0",
        "jest": "^29.5.0",
        "prettier": "3.0.0",
        "stylelint-config-prettier": "^9.0.5",
        "supertest": "^6.3.3",
        "ts-jest": "^29.1.1",
        "ts-node-dev": "^2.0.0",
        "typescript": "^5.1.6"
    }
}

Hey there,

I see you’re having an issue with deploying your app. Would you mind providing the logs/stacktrace in your terminal that was given before the error- Error: failed to fetch an image or build from source: error building: failed to solve: executor failed running [/bin/sh -c npm run build]: exit code: 1. It would help provide more context to the situation going on.

2 Likes

Sure, here’s what appears before the error in the terminal:

Validating /Users/[username]/Coding/projects/ladybug/server/fly.toml
Platform: machines
✓ Configuration is valid
--> Verified app config
==> Building image
Remote builder fly-builder-weathered-wave-924 ready
==> Building image with Docker
--> docker host: 20.10.12 linux x86_64
[+] Building 3.2s (11/12)                                                                                 
 => [internal] load build definition from Dockerfile                                                 0.6s
 => => transferring dockerfile: 32B                                                                  0.6s
 => [internal] load .dockerignore                                                                    0.6s
 => => transferring context: 35B                                                                     0.6s
 => [internal] load metadata for docker.io/library/node:16.13.2-slim                                 0.6s
 => [internal] load build context                                                                    0.6s
 => => transferring context: 494B                                                                    0.6s
 => [base 1/2] FROM docker.io/library/node:16.13.2-slim@sha256:f527a6118422b888c35162e0a7e2fb2febce  0.0s
 => CACHED [base 2/2] WORKDIR /app                                                                   0.0s
 => CACHED [build 1/5] RUN apt-get update -qq &&     apt-get install -y python pkg-config build-ess  0.0s
 => CACHED [build 2/5] COPY package-lock.json package.json ./                                        0.0s
 => CACHED [build 3/5] RUN npm ci                                                                    0.0s
 => CACHED [build 4/5] COPY . .                                                                      0.0s
 => ERROR [build 5/5] RUN npm run build                                                              1.4s
------
 > [build 5/5] RUN npm run build:
#11 1.103 
#11 1.103 > server@1.0.0 build
#11 1.103 > tsc
#11 1.103 
#11 1.328 Version 5.1.6
#11 1.331 tsc: The TypeScript Compiler - Version 5.1.6
#11 1.331 
#11 1.331 COMMON COMMANDS
#11 1.331 
#11 1.331   tsc
#11 1.331   Compiles the current project (tsconfig.json in the working directory.)
#11 1.331 
#11 1.332   tsc app.ts util.ts
#11 1.332   Ignoring tsconfig.json, compiles the specified files with default compiler options.
#11 1.332 
#11 1.332   tsc -b
#11 1.332   Build a composite project in the working directory.
#11 1.332 
#11 1.332   tsc --init
#11 1.332   Creates a tsconfig.json with the recommended settings in the working directory.
#11 1.332 
#11 1.332   tsc -p ./path/to/tsconfig.json
#11 1.332   Compiles the TypeScript project located at the specified path.
#11 1.332 
#11 1.332   tsc --help --all
#11 1.332   An expanded version of this information, showing all possible compiler options
#11 1.332 
#11 1.332   tsc --noEmit
#11 1.332   tsc --target esnext
#11 1.332   Compiles the current project, with additional settings.
#11 1.332 
#11 1.332 COMMAND LINE FLAGS
#11 1.332 
#11 1.332 --help, -h
#11 1.332 Print this message.
#11 1.332 
#11 1.332 --watch, -w
#11 1.332 Watch input files.
#11 1.332 
#11 1.332 --all
#11 1.332 Show all compiler options.
#11 1.332 
#11 1.332 --version, -v
#11 1.332 Print the compiler's version.
#11 1.332 
#11 1.332 --init
#11 1.332 Initializes a TypeScript project and creates a tsconfig.json file.
#11 1.332 
#11 1.332 --project, -p
#11 1.332 Compile the project given the path to its configuration file, or to a folder with a 'tsconfig.json'.
#11 1.332 
#11 1.333 --build, -b
#11 1.333 Build one or more projects and their dependencies, if out of date
#11 1.333 
#11 1.333 --showConfig
#11 1.333 Print the final configuration instead of building.
#11 1.333 
#11 1.333 COMMON COMPILER OPTIONS
#11 1.333 
#11 1.333 --pretty
#11 1.333 Enable color and formatting in TypeScript's output to make compiler errors easier to read.
#11 1.333 type: boolean
#11 1.333 default: true
#11 1.333 
#11 1.333 --declaration, -d
#11 1.333 Generate .d.ts files from TypeScript and JavaScript files in your project.
#11 1.333 type: boolean
#11 1.333 default: `false`, unless `composite` is set
#11 1.333 
#11 1.333 --declarationMap
#11 1.333 Create sourcemaps for d.ts files.
#11 1.334 type: boolean
#11 1.334 default: false
#11 1.334 
#11 1.334 --emitDeclarationOnly
#11 1.334 Only output d.ts files and not JavaScript files.
#11 1.334 type: boolean
#11 1.334 default: false
#11 1.334 
#11 1.334 --sourceMap
#11 1.334 Create source map files for emitted JavaScript files.
#11 1.334 type: boolean
#11 1.334 default: false
#11 1.334 
#11 1.334 --target, -t
#11 1.334 Set the JavaScript language version for emitted JavaScript and include compatible library declarations.
#11 1.335 one of: es3, es5, es6/es2015, es2016, es2017, es2018, es2019, es2020, es2021, es2022, esnext
#11 1.335 default: es5
#11 1.335 
#11 1.335 --module, -m
#11 1.335 Specify what module code is generated.
#11 1.335 one of: none, commonjs, amd, umd, system, es6/es2015, es2020, es2022, esnext, node16, nodenext
#11 1.335 default: undefined
#11 1.335 
#11 1.335 --lib
#11 1.335 Specify a set of bundled library declaration files that describe the target runtime environment.
#11 1.335 one or more: es5, es6/es2015, es7/es2016, es2017, es2018, es2019, es2020, es2021, es2022, es2023, esnext, dom, dom.iterable, webworker, webworker.importscripts, webworker.iterable, scripthost, es2015.core, es2015.collection, es2015.generator, es2015.iterable, es2015.promise, es2015.proxy, es2015.reflect, es2015.symbol, es2015.symbol.wellknown, es2016.array.include, es2017.object, es2017.sharedmemory, es2017.string, es2017.intl, es2017.typedarrays, es2018.asyncgenerator, es2018.asynciterable/esnext.asynciterable, es2018.intl, es2018.promise, es2018.regexp, es2019.array, es2019.object, es2019.string, es2019.symbol/esnext.symbol, es2019.intl, es2020.bigint/esnext.bigint, es2020.date, es2020.promise, es2020.sharedmemory, es2020.string, es2020.symbol.wellknown, es2020.intl, es2020.number, es2021.promise/esnext.promise, es2021.string, es2021.weakref/esnext.weakref, es2021.intl, es2022.array, es2022.error, es2022.intl, es2022.object, es2022.sharedmemory, es2022.string/esnext.string, es2022.regexp, es2023.array/esnext.array, esnext.intl, decorators, decorators.legacy
#11 1.335 default: undefined
#11 1.335 
#11 1.335 --allowJs
#11 1.335 Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files.
#11 1.335 type: boolean
#11 1.335 default: false
#11 1.335 
#11 1.335 --checkJs
#11 1.336 Enable error reporting in type-checked JavaScript files.
#11 1.336 type: boolean
#11 1.336 default: false
#11 1.336 
#11 1.336 --jsx
#11 1.336 Specify what JSX code is generated.
#11 1.336 one of: preserve, react, react-native, react-jsx, react-jsxdev
#11 1.336 default: undefined
#11 1.336 
#11 1.336 --outFile
#11 1.336 Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output.
#11 1.336 
#11 1.336 --outDir
#11 1.336 Specify an output folder for all emitted files.
#11 1.336 
#11 1.336 --removeComments
#11 1.336 Disable emitting comments.
#11 1.336 type: boolean
#11 1.336 default: false
#11 1.336 
#11 1.336 --noEmit
#11 1.336 Disable emitting files from a compilation.
#11 1.336 type: boolean
#11 1.336 default: false
#11 1.336 
#11 1.336 --strict
#11 1.336 Enable all strict type-checking options.
#11 1.336 type: boolean
#11 1.336 default: false
#11 1.336 
#11 1.336 --types
#11 1.336 Specify type package names to be included without being referenced in a source file.
#11 1.336 
#11 1.336 --esModuleInterop
#11 1.336 Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility.
#11 1.336 type: boolean
#11 1.336 default: false
#11 1.336 
#11 1.336 You can learn about all of the compiler options at https://aka.ms/tsc
#11 1.336 
------
Error: failed to fetch an image or build from source: error building: failed to solve: executor failed running [/bin/sh -c npm run build]: exit code: 1
2 Likes

Thanks for providing that!

So the error is in regards to npm run build not succeeding. Looks like it’s an issue with either dockerfile or the build setup. Considering all the tsc help text, I would say it has something to do with either a missing tsconfig.json file or missing arguments to tsc (the TypeScript compiler). I would take a look at this documentation here, hopefully it helps!

1 Like

Thanks! There was an issue with my tsconfig file. Seems to have resolved the issue

1 Like

Awesome! Thanks for updating us and letting us all know, it helps!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.