Error: Cannot find module '/opt/render/project/src/app.js'

Why is it showing this Error: Cannot find module ‘/opt/render/project/src/app.js’. ?
My package.json file is:
{
“version”: “1.0.0”,
“main”: “app.js”,
“scripts”: {
“start”: “nodemon app.js”
},
“keywords”: ,
“dependencies”: {
“ejs”: “^3.1.6”,
“express”: “^4.17.1”,
“mongodb”: “^4.10.0”
},
“devDependencies”: {
“nodemon”: “^2.0.19”
},
“name”: “blog_project_crud_operations_mongodb”,
“author”: “Hitesh Mishra”,
“description”: “”
}

I am using " npm install " as build command and " node app.js " as start command.

This is my github repo which i want to deploy:
https://github.com/hiteshmishra2103/web-dev/tree/main/blog_project_crud_operations_mongodb

Hi there,

Thanks for reaching out.

If you’re trying to deploy part of a monorepo project, it sounds like you may need to set the “Root Directory” to blog_project_crud_operations_mongodb in the service settings.

Hope that helps

Alan

Thank you! It is not showing the previous error but now it is showing this error: UnhandledPromiseRejectionWarning: MongoAPIError: URI must include hostname, domain name, and tld

However I used this as MONGODB_URI in environment variables mongodb+srv://myaccountname:@mydb.s3umjey.mongodb.net/?retryWrites=true&w=majority

It is still showing the error connecting to db.

This is my database.js

const mongodb = require(“mongodb”);

const MongoClient = mongodb.MongoClient;

let database;

let mongodbUrl=‘mongodb://127.0.0.1:27017’;

if(process.env.MONGODB_URI){
mongodbUrl=process.env.MONGODB_URI;
}

async function connect() {
const client = await MongoClient.connect(mongodbUrl);
//for establishing the connection to the server as whole

database = client.db(“blog”);
}

The error shows it’s trying to connect to 127.0.0.1:27017 which is incorrect, you’ll need to ensure your code is reading from the environment variable.

Alan

I have added environment variable MONGODB_URI while deploying and this is the code of database.js.:point_down: Can you please tell where the mistake can be and how can i ensure that my code is reading from the environment variable?

const mongodb = require(“mongodb”);

const MongoClient = mongodb.MongoClient;

let database;

let mongodbUrl=‘mongodb://127.0.0.1:27017’;

if(process.env.MONGODB_URI){
mongodbUrl=process.env.MONGODB_URI;
}

async function connect() {
const client = await MongoClient.connect(mongodbUrl);
//for establishing the connection to the server as whole

database = client.db(“blog”);
}

It looks like that conditional isn’t overwriting the localhost IP for some reason.

This looks like a code issue and we’re not able to support your code, as every app is different.

Consider adding some debugging/logging to ensure the env var is available and as you expect.

Alan

Thanks Alan it is working fine now!

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