Question

[Error: Cannot find module '/workspace/dist/server.js'] Unable to Deploy NodeJS App on Application Platform.

Hey everyone. I’m trying to deploy my application but nothing seems to work. I’ve tried the example repository and changed the path to ./bin/www/dist/server.js, but that doesn’t seem to work too.

here are my scripts:

"scripts": {
    "start": "NODE_ENV=production node ./dist/server.js",
    "dev": "nodemon server.js --exec babel-node",
    "build": "babel server.js -d dist",
    "build:digitalocean": "npm install --production=false && npm run build && npm ci && npm run start"
},

And here are the logs

[2024-03-07 10:24:22] 
[2024-03-07 10:24:22] > server@1.0.0 start
[2024-03-07 10:24:22] > NODE_ENV=production node ./dist/server.js
[2024-03-07 10:24:22] 
[2024-03-07 10:24:23] node:internal/modules/cjs/loader:1147
[2024-03-07 10:24:23]   throw err;
[2024-03-07 10:24:23]   ^
[2024-03-07 10:24:23] 
[2024-03-07 10:24:23] Error: Cannot find module '/workspace/dist/server.js'
[2024-03-07 10:24:23]     at Module._resolveFilename (node:internal/modules/cjs/loader:1144:15)
[2024-03-07 10:24:23]     at Module._load (node:internal/modules/cjs/loader:985:27)
[2024-03-07 10:24:23]     at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:135:12)
[2024-03-07 10:24:23]     at node:internal/main/run_main_module:28:49 {
[2024-03-07 10:24:23]   code: 'MODULE_NOT_FOUND',
[2024-03-07 10:24:23]   requireStack: []
[2024-03-07 10:24:23] }
[2024-03-07 10:24:23] 
[2024-03-07 10:24:23] Node.js v20.10.0
[2024-03-07 10:24:16] 
[2024-03-07 10:24:16] > server@1.0.0 start
[2024-03-07 10:24:16] > NODE_ENV=production node ./dist/server.js
[2024-03-07 10:24:16] 
[2024-03-07 10:24:16] node:internal/modules/cjs/loader:1147
[2024-03-07 10:24:16]   throw err;
[2024-03-07 10:24:16]   ^
[2024-03-07 10:24:16] 
[2024-03-07 10:24:16] Error: Cannot find module '/workspace/dist/server.js'
[2024-03-07 10:24:16]     at Module._resolveFilename (node:internal/modules/cjs/loader:1144:15)
[2024-03-07 10:24:16]     at Module._load (node:internal/modules/cjs/loader:985:27)
[2024-03-07 10:24:16]     at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:135:12)
[2024-03-07 10:24:16]     at node:internal/main/run_main_module:28:49 {
[2024-03-07 10:24:16]   code: 'MODULE_NOT_FOUND',
[2024-03-07 10:24:16]   requireStack: []
[2024-03-07 10:24:16] }
[2024-03-07 10:24:16] 
[2024-03-07 10:24:16] Node.js v20.10.0
[]

Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
March 7, 2024
Accepted Answer

Hey!

The error “Cannot find module ‘/workspace/dist/server.js’” indicates that Node.js is unable to locate the server.js file in the dist directory when trying to execute it.

This issue is usually related to the build and deployment process not completing as expected. Here are some steps to troubleshoot and potentially resolve the issue:

You need to make sure that the build script in your package.json correctly compiles your source code to the dist directory. The script "build": "babel server.js -d dist" should generate the server.js file in the dist folder.

Before deploying, locally run your build script and check if server.js is successfully generated in the dist directory.

Your build:digitalocean script is trying to install dependencies, build the project, install production dependencies only, and then start the app. This sequence seems a bit off. Typically, you’d build your app and then install only the production dependencies. Consider revising it to something like:

"build:digitalocean": "npm install --production=false && npm run build && npm prune --production && npm run start"

This ensures that after the build, only necessary production dependencies are retained.

Also, make sure that the DigitalOcean App Platform is configured to execute the build:digitalocean script during the build stage. The platform should be set to use this specific build command to build your app correctly.

If none of the above helps, you should double-check that the file path in the start script (./dist/server.js) is correct relative to the root of your project on the DigitalOcean App Platform.

Let me know how it goes!

Best,

Bobby

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.