Is this a Vercel bug? Cannot find module './model'

Viewed 501

I have 2 files:

  • controller.js
  • model.js

I'm making an express.js app.

So, model.js is required inside controller.js but I have this error when I call my api image

And the logs are:

image

But here is the problem, './model.js' really does exist, but vercel doesn't recognize it And works fine in local development and it is required correctly

this is model.js

const { nanoid } = require("nanoid");

const getDateStr = () => {
    const now = new Date();
    return `${now.getFullYear()}-${now.getMonth()+1}-${now.getDate()}`;
}

const Purchase = ({ id_user_buyer, id_user_seller, id_product, quantity }) => (
    {
        id_user_buyer,
        id_user_seller,
        id_product,
        quantity,
        id_purchase: nanoid(),
        date: getDateStr(),
    }
);

module.exports = {
    Purchase
}

And this is controller.js

const err = require("../../../utils/error");
const { Purchase } = require("./model")
// others modules and they are well imported, so the problem is './model'
const userController = require("../user");
const productController = require("../product");
const cartController = require("../cart");

const TABLE = 'purchase';

function purchaseController(injectedStore) {
    // example code
    async function makePurchase(data) {
        const purchase = Purchase(data);
        await injectedStore.insert(TABLE, purchase);
    }
    
    return {
        makePurchase,
    }
}

module.exports = purchaseController;

As you can see, model.js is well imported inside controller.js I don't know why vercel says ERROR Cannot find module './model' I say it again, works fine in local development but not in vercel

A quick fix is copy and paste all the code of model.js inside controller.js I tried it, I deploy him and it worked. All my app also works fine if I just comment that line where I import ./model , but obviously my application would stop having that functionality, so the first solution is uggly but works, but those are not the best solutions. the best solution is for the file to be imported correctly

Curious fact I tried renaming the file and it didn't help either. It also doesn't work if I import a new file.

NOTE I changed the nodejs version from 12 to 14, will that have something to do with it?

just in case I put my folder structure

root

image

api

image

this is my vercel.json

{
    "version": 2,
    
    "builds": [
        {
            "src": "/api/index.js",
            "use": "@vercel/node"
        }
    ],

    "routes": [
        {
            "src": "/api/auth(.*)",
            "dest": "/api/index.js"
        },
        {
            "src": "/api/users(.*)",
            "dest": "/api/index.js"
        },
        {
            "src": "/api/products(.*)",
            "dest": "/api/index.js"
        },
        {
            "src": "/api/cart(.*)",
            "dest": "/api/index.js"
        },
        {
            "src": "/api/purchases(.*)",
            "dest": "/api/index.js"
        },
        {
            "src": "/api/sales(.*)",
            "dest": "/api/index.js"
        }
    ]
}

I don't know if it's a vercel bug or it's a mistake on my part. My application works currently but doing the trick that I named before, which was about putting all the code of model.js inside of controller.js

Thanks for reading the whole issue.

1 Answers

Well, I don't know why, but i think was a vercel bug, the solution for my problem was not to use optional chaining.

In controller.js I have more code and I was using optional chaining, just change the logic and works fine. Vercel supports optional chaining but with node 14.x and I was using node 12.x in vercel, so I change it to 14.x and works fine in others files, but not in controller.js

So, if you are using javascript new things, like optional chaining, nullish operator, etc. And you are using version 12.x in vercel, that will produce errors. So you change to node 14.x, it could give you bugs too.

So the best you can do is make sure from the beginning that you have version 14.x of node.js in vercel

how to change node version in vercel

Related