"Persistent 'Cannot find module 'dotenv/config'' Error Despite Correct Dockerfile Configuration

crinklepackageHOBBY

3 months ago

I'm able to successfuly build my api service in my shared monorepo, but the deploy fails with this error 'Cannot find module 'dotenv/config'

I've got the variables set up in railway.

the top line of my index.ts file is
import 'dotenv/config';

I'll attach relevant files in a subsequent comment.

Awaiting User Response

7 Replies

crinklepackageHOBBY

3 months ago

Attached: dockerfile, screenshot of my variables in railway.

I've also got dotenv defined in my api packages.json file, below (but not in the root packages.json file)
`

  "dependencies": {
    "@supabase/supabase-js": "^2.39.0",
    "@wavenotes/shared": "workspace:^",
    "bullmq": "^5.4.2",
    "cors": "^2.8.5",
    "dotenv": "^16.4.7",

Attachments

Dockerfile

3 months ago

Hello,

You have a root directory set, once set you cannot access anything outside that directory.


Status changed to Awaiting User Response railway[bot] 3 months ago


crinklepackageHOBBY

3 months ago

Thanks. That is getting me closer to the solution I think… I've tried for a bunch of different solves here but am stuck.

I'm able to build locally and build on Railway, but the deploy crashes on startup with Cannot find module 'dotenv/config'. I'm using Yarn workspaces and a multi-stage Docker build. I've tried copying the .env file and installing production dependencies in the runtime stage, but the issue persists. Are there any known issues with dependency hoisting or caching within Railway's build environment that might be preventing dotenv from being correctly resolved at runtime, even though it's listed as a production dependency in my package.json?"

Current api dockerfile is attached.

Attachments


Status changed to Awaiting Railway Response railway[bot] 3 months ago


3 months ago

You can modify your code to not even import that module as there is no need for that module on Railway.


Status changed to Awaiting User Response railway[bot] 3 months ago


crinklepackageHOBBY

3 months ago

Thank you. I dealt with that, and now I'm getting this deploy crash. Any idea about what might be happening?

Error: Cannot find module 'express' Require stack: - /app/dist/index.js ... (rest of stack trace)

Here's a summary of the problem and the troubleshooting steps I've already taken:

  • Intermittent Nature: The error does not occur consistently. Sometimes the deployment succeeds, and sometimes it fails with this "Cannot find module 'express'" error.

  • Local Docker Build: My local Docker builds are successful. The application runs correctly when built and run locally using Docker. This indicates that the express dependency is correctly listed in my package.json and installed during the Docker build process.

  • Dockerfile: I am using a multi-stage Docker build. I install all dependencies in the build stage and then copy the node_modules folder, package.json, and yarn.lock to the runtime stage. I then install only production dependencies in the runtime stage using yarn install --production.

  • Dependency Management: I am using Yarn 4 for dependency management.

  • Troubleshooting Steps:

    • I have tried clearing the build cache on Railway by making small, insignificant changes to my code and redeploying. This sometimes works, but the issue recurs.

    • I have verified that express is listed as a dependency in my package.json file.

    • I have tried regenerating my yarn.lock file locally and committing the changes.

    • I have tried rebuilding the Docker image locally multiple times.

    • I have tried different variations of my Dockerfile (including installing dependencies in the runtime stage). However, these changes have either broken my local build or not resolved the issue on Railway.

Attachments


Status changed to Awaiting Railway Response railway[bot] 3 months ago


crinklepackageHOBBY

3 months ago

I'l create a new issue as this is a different error message.


3 months ago

Doesn't look like you are copying node_modules into the runtime stage, perhaps it would be best for you to remove the runtime stage stuff and just have a single stage for simplicity.

And please do not commit your .env files to GtiHub.


Status changed to Awaiting User Response railway[bot] 3 months ago


"Persistent 'Cannot find module 'dotenv/config'' Error Despite Correct Dockerfile Configuration - Railway Help Station