# Module "express" not found when trying to deploy Express application

**URL:** <https://community.fly.io/t/module-express-not-found-when-trying-to-deploy-express-application/12758>\
**Category:** Build debugging\
**Created:** [May 7, 2023, 2:03pm UTC](https://community.fly.io/t/module-express-not-found-when-trying-to-deploy-express-application/12758 "2023-05-07T14:03:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![pebbletech](https://avatars.discourse-cdn.com/v4/letter/p/d6d6ee/32.png) [@pebbletech](https://community.fly.io/u/pebbletech)\
**Post date:** [May 7, 2023, 2:03pm UTC](https://community.fly.io/t/module-express-not-found-when-trying-to-deploy-express-application/12758/1 "2023-05-07T14:03:57Z")

</div>

Hi,

I have a monorepo setup for my application and was trying to deploy my Express API server to production but no luck getting it running from Fly instance ☹

I am able to complete Docker build step and deploying to Fly instance but it’s throwing me an error when trying to start the server stating `Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'express' imported from /app/src/server.js`. I did a `ls node_modules` in my `start` command and found all the modules are actually there in the server.

I setup my monorepo using pnpm workspace and turborepo. My monorepo folder structure is as below:

```auto
>apps
  > server
    > api // All my api code is here
        > server.js // Classic express server setup
        > package.json // Package.json for my api server
> packages
  > //... Internal packages
package.json // Package.json shared in monorepo
.pnpm-lock.yaml // Lock file for monorepo, only have one
fly.toml
Dockerfile
.dockerignore

```

As you can see, I need to put `fly.toml` , `Dockerfile` and `.dockerignore` in root as we have shared packages that need to be resolved by `pnpm`.

I have configure the default `fly.toml`, `Dockerfile` to match my monorepo from the default generated template as the following:

**fly.toml**

```toml
# fly.toml app configuration file generated for casa-mono-api on 2023-05-07T18:41:20+08:00
#
# See https://fly.io/docs/reference/configuration/ for information about how to use this file.
#

app = "my-api"
primary_region = "sin"

[env]
  PORT = "6060"

[http_service]
  internal_port = 6060
  force_https = true
  auto_stop_machines = true
  auto_start_machines = true

```

**Dockerfile**

```plaintext
# syntax = docker/dockerfile:1

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

LABEL fly_launch_runtime="NodeJS"

# NodeJS app lives here
WORKDIR /app

RUN npm install -g pnpm

# Set production environment
ENV NODE_ENV=production

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

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

COPY --link . .

# Install node modules
RUN pnpm i --filter api

# Final stage for app image
FROM base

# Copy built application
COPY --from=build /app/apps/server/api /app

# Start the server by default, this can be overwritten at runtime
CMD ["pnpm", "run", "start"]

```

**package.json (of apps/server/api)**

```json
{
  "name": "api",
  "version": "2.0.0",
  "main": "index.js",
  "type": "module",
  "scripts": {
    "start": "ls node_modules && node src/server.js",
    "dev": "nodemon src/server.js",
    "init": "node ./scripts/init.js"
  },
  "author": "me",
  "license": "ISC",
  "devDependencies": {
    "eslint-config-standard": "^17.0.0",
    "eslint-plugin-import": "^2.22.1",
    "eslint-plugin-node": "^11.1.0",
    "eslint-plugin-promise": "^6.1.1",
    "nodemon": "2.0.22"
  },
  "dependencies": {
    "axios": "^1.3.5",
    "bcryptjs": "2.4.3",
    "body-parser": "1.20.2",
    "dayjs": "1.11.7",
    "express": "4.18.1",
    "express-http-context": "^1.2.4",
    "jsonwebtoken": "9.0.0",
    "mailgun-js": "0.22.0",
    "mongoose": "7.0.3",
    "mongoose-long": "0.6.0",
    "morgan": "1.10.0",
    "passport": "0.6.0",
    "passport-jwt": "4.0.1",
    "uuid": "9.0.0",
    "winston": "3.8.2",
    "constant-utils": "workspace:*",
    "helper-utils": "workspace:*"
  },
  "lint-staged": {
    "*.js": "prettier --write"
  }
}

```

I’m not sure what I did wrong in any of the steps as I am new to using Docker and Fly and there is limited amount of doc for monorepo Express API setup for [fly.io](http://fly.io). Appreciate if anyone can help.

---

<div class="post-metadata">

**Author:** ![rubys](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/rubys/32/16907_2.png) [@rubys](https://community.fly.io/u/rubys)\
**Post date:** [May 7, 2023, 3:15pm UTC](https://community.fly.io/t/module-express-not-found-when-trying-to-deploy-express-application/12758/2 "2023-05-07T15:15:41Z")

</div>

> [@pebbletech](#):
>
> `COPY --from=build /app/apps/server/api /app`

I’m not (yet!) familiar enough with pnpm’s inner workings, but I suspect that there is a full path someplace internally when dealing with monoreps.

It looks like you are only deploying one app. If so, perhaps instead of:

```dockerfile
COPY --link . .
RUN pnpm i --filter api
...
COPY --from=build /app/apps/server/api /app

```

You could try:

```dockerfile
COPY --link apps/server/api .
RUN pnpm i
...
COPY --from=build /app /app

```

Another approach is to copy the whole tree and then change the workdir. So replace:

```auto
COPY --from=build /app/apps/server/api /app

```

with

```auto
COPY --from=build /app /app
WORKDIR /app/apps/server/api

```

---

<div class="post-metadata">

**Author:** ![pebbletech](https://avatars.discourse-cdn.com/v4/letter/p/d6d6ee/32.png) [@pebbletech](https://community.fly.io/u/pebbletech)\
**Post date:** [May 7, 2023, 4:20pm UTC](https://community.fly.io/t/module-express-not-found-when-trying-to-deploy-express-application/12758/3 "2023-05-07T16:20:22Z")

</div>

Hey,

Thanks for your reply! The below works:

> [@rubys](#):
>
> ```auto
> COPY --from=build /app /app
> WORKDIR /app/apps/server/api
> 
> ```

Apparently pnpm install everything in root `node_modules/.pnpm` and create a symbolic link to the module from workspace `node_modules`.

---

<div class="post-metadata">

**Author:** ![pebbletech](https://avatars.discourse-cdn.com/v4/letter/p/d6d6ee/32.png) [@pebbletech](https://community.fly.io/u/pebbletech)\
**Post date:** [May 7, 2023, 4:21pm UTC](https://community.fly.io/t/module-express-not-found-when-trying-to-deploy-express-application/12758/4 "2023-05-07T16:21:25Z")

</div>

> [@rubys](#):
>
> ```auto
> COPY --link apps/server/api .
> RUN pnpm i
> ...
> COPY --from=build /app /app
> 
> ```

This wouldn’t work as I tried before. It needs to have access to root folder because `.pnpm-lock` is in there

---

<div class="post-metadata">

**Author:** ![rubys](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/rubys/32/16907_2.png) [@rubys](https://community.fly.io/u/rubys)\
**Post date:** [May 7, 2023, 5:06pm UTC](https://community.fly.io/t/module-express-not-found-when-trying-to-deploy-express-application/12758/5 "2023-05-07T17:06:01Z")

</div>

> [@pebbletech](#):
>
> there is limited amount of doc for monorepo Express API setup for [fly.io](http://fly.io).

I’m glad you are working, and I’ve learned something in the process. Thank you! I’ve opened [Better support for monorepos](https://github.com/fly-apps/dockerfile-node/issues/15) to track the changes needed to better support your use case.

My goal is to make the dockerfile we generate either “just work”, and when that is not possible allow customization by rerunning `npx dockerfile` with flags. Feel free to take a look at the dockerfile-node repository, open issues and (if you are so inclined) pull requests. The code is pretty straightforward, mostly a single class, yargs, and ejs templates.

---

<div class="post-metadata">

**Author:** ![pebbletech](https://avatars.discourse-cdn.com/v4/letter/p/d6d6ee/32.png) [@pebbletech](https://community.fly.io/u/pebbletech)\
**Post date:** [May 8, 2023, 7:58am UTC](https://community.fly.io/t/module-express-not-found-when-trying-to-deploy-express-application/12758/6 "2023-05-08T07:58:04Z")

</div>

Hey @rubys ,

May I know what’s the right way to incorporate process manager like [PM2](https://pm2.io/) for node to manage our node.js process? It includes stuff like auto-restart, good enough dashboard, and also concurrent mode for application (if we have \>1 CPU in the instance)
