1

I am trying to deploy a MERN App on render. The build gives this error.

../backend/src/models/hotel.ts(1,22): error TS2307: Cannot find module 'mongoose' or its corresponding type declarations.
../backend/src/models/user.ts(1,22): error TS2307: Cannot find module 'mongoose' or its corresponding type declarations.
../backend/src/models/user.ts(2,20): error TS2307: Cannot find module 'bcryptjs' or its corresponding type declarations.
../backend/src/models/user.ts(22,8): error TS2683: 'this' implicitly has type 'any' because it does not have a type annotation.
../backend/src/models/user.ts(23,9): error TS2683: 'this' implicitly has type 'any' because it does not have a type annotation.
../backend/src/models/user.ts(23,43): error TS2683: 'this' implicitly has type 'any' because it does not have a type annotation.

The user.ts file is:

import mongoose from "mongoose";
import bcrypt from "bcryptjs";

export type UserType = {
    _id: string;
    email: string;
    password: string;
    firstName: string;
    lastName: string;
};

const userSchema = new mongoose.Schema({
    email:{type:String,required:true,unique:true},
    password:{type:String,required:true},
    firstName:{type:String,required:true},
    lastName:{type:String,required:true}
});

//function to be executed before saving the user to the database
//this is actually a middleware
userSchema.pre("save", async function(next: mongoose.CallbackWithoutResultAndOptionalError){
    if(this.isModified("password")){
        this.password = await bcrypt.hash(this.password,10);
    }
    next();
});

const User = mongoose.model<UserType>("User",userSchema);

export default User;

I have the mongoose and bycryptjs types installed via this command:

npm install --save-dev @types/mongoose @types/bcryptjs

my tsconfig.json file includes:

{
  "compilerOptions": {
    "types": ["node"]
  }
}

When I hover over this, my VS Code gives me the following

this: mongoose.Document<unknown, {}, mongoose.FlatRecord<{
    firstName: string;
    lastName: string;
    email: string;
    password: string;
}>> & mongoose.FlatRecord<{
    firstName: string;
    lastName: string;
    email: string;
    password: string;
}> & {
    ...;
}

These are my versions:

  "@types/mongodb": "^3.6.8",
  "@types/mongoose": "^5.11.97",
  "@types/bcryptjs": "^2.4.6",
"bcryptjs": "^2.4.3",
"mongodb": "^6.3.0",
"mongoose": "^6.0.12",

This is my build command on render.

cd frontend && npm install && npm run build && cd ../backend && npm run build

But I am still getting this error and I don't know what to do.

1 Answer 1

0

It's more a npm installation related error. In the render.com settings of your project you should have a build command like that:

npm install && npm run build

enter image description here

for my project i have src/ relative path, don't pay attention.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.