Cannot find module dotenv

I was finally able to deploy my application to Heroku, but there’s a new problem. When I try to view the app I get a page that says “Application Error”. When I open the heroku logs, it says “Cannot find module dotenv”.

But I do have dotenv in my package.json and my package-lock.json

I figured the problem was that dotenv was listed as a devDependency so I tried npm i dotenv and even npm i -D dotenv. I also made sure to restart VSC afterwards, but nothing seems to be getting rid of the error.

Right, it should definitely be used in your dependencies, not dev. But when I look in your package.json:

  "dependencies": {
    "axios": "^0.27.2",
    "express": "^4.18.1",
    "mongoose": "^6.5.0",
    "react-icons": "^4.4.0"
  },
  "devDependencies": {
    "dotenv": "^16.0.1",
    "nodemon": "^2.0.19"
  },

And I’m not sure what “I also made sure to restart VSC afterwards” will do - the issue is heroku - you have to remember to redeploy there.

Some of the other forums suggested we restart our code editors once we retry installing dotenv. Redeploying hasn’t worked out yet.

As for putting dotenv in the dependency section; should I just copy paste it from dev dependencies, then hit npm install? I tried doing npm install dotenv to begin with, but it didn’t work

Either should work. You can do npm i dotenv" or manually edit it and npm i`. The important thing is that you have it in your dependencies and not dev dependencies.
If you want to be extra sure, delete package-lock.json and the node_modules folder before installing. Then you need to push that to heroku.

After deleting the folders, should I reinstall them first before pushing, or no?

SUCCESS!!

Now I just need to hire a web design expert lol!

1 Like

reinstalling before push … yes and no. It won’t matter for the node_modules because you aren’t pushing them anyway. But it does matter for the lock file - that gets pushed and is important so it knows exactly which version to use for consistency. So, yes, install before push.

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