Node.js2 minutes to read

How do I deploy a TypeScript API that builds into dist?

Choose where to compile a TypeScript backend, keep the compiler available during the build, and point Node.js at the JavaScript file the build produced.

Identify the JavaScript entry point

For a TypeScript API that normally runs compiled JavaScript, deploy the build output or compile during startup. Node.js should start the file your compiler emitted. Suppose your API lives in src/server.ts. With rootDir set to src and outDir set to dist, that source typically produces dist/server.js. Verify the path in a fresh local build. TypeScript’s output directory reference.

Compile from source on the deployment

Upload your source, package manifest, lockfile and tsconfig.json. In this example, the package has a build script that runs TypeScript and a start script that runs node dist/server.js. If TypeScript is in devDependencies, include development packages when compiling. Use the following RUN_COMMAND on the Node.js HTTP profile.

Install the compiler, build, then start
npm ci --include=dev && npm run build && npm start

If the compiler reports an error, fix that first. If compilation succeeds but Node reports a missing module at startup, inspect the actual contents of dist. A configuration that preserves an extra source directory can emit dist/src/server.js. Check the build against the exact project revision that reached the deployment.

When a local build is a better fit

Compiling can use more memory and disk than serving requests. Build on your machine or in your own CI, upload the generated JavaScript and any assets the server reads, and include the manifest and lockfile for runtime dependencies. Match the target Node version and let the host install native dependencies for its operating system. For this package the prebuilt startup command becomes npm ci --omit=dev && npm start.

Request a route whose response changed in the new build. A successful startup alone can also happen with yesterday’s compiled files. Node.js deployment and route checks.