Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'dotenv' imported from /opt/render/project/src/backend/server.js

Got this error a couple days ago after doing a routine update to a .env variable on Render. The branch would always deploy with no issues even just a month ago, but started prompting this error recently. Local deploy works. Nothing in the codebase has changed except the .env on Render. Also, for some reason the branch would successfully deploy when doing a rollback to a previous deployment of that same branch… dotenv IS in my dependencies in package.json and dotenv IS being called properly from my server.js from what I understand. Am I overlooking something?

Some things I’ve tried:

  • Different Node versions using NODE_VERSION in .env
  • Deleted node_modules and did new npm install
  • Revert .env back to previous setup

server.js (dotenv)

import path from ‘path’;

import express from ‘express’;

import dotenv from ‘dotenv’;

import cors from ‘cors’;

import nodemailer from ‘nodemailer’;

import cookieParser from ‘cookie-parser’;

dotenv.config();

package.json

{
“name”: “frame_coffee”,
“version”: “1.0.0”,
“description”: “FRAME coffee roasters main website/ ecommerce store built using MERN stack”,
“type”: “module”,
“main”: “server.js”,
“scripts”: {
“postinstall”: “npm install dotenv”,
“start”: “node backend/server.js”,
“server”: “nodemon backend/server.js”,
“client”: “npm start --prefix frontend”,
“dev”: "concurrently "npm run server" "npm run client" ",
“data:import”: “node backend/seeder.js”,
“data:destroy”: “node backend/seeder.js -d”,
“build”: “npm install && npm install --prefix frontend && npm run build --prefix frontend”
},
“repository”: {
“type”: “git”,
“url”: “git+https://github.com/yshin22/framecoffee.git”
},
“author”: “”,
“license”: “MIT”,
“bugs”: {
“url”: “Issues · yshin22/framecoffee · GitHub
},
“homepage”: “GitHub - yshin22/framecoffee”,
“dependencies”: {
“bcryptjs”: “^2.4.3”,
“colors”: “^1.4.0”,
“cookie-parser”: “^1.4.6”,
“cors”: “^2.8.5”,
“express”: “^4.18.2”,
“jsonwebtoken”: “^9.0.2”,
“mongoose”: “^7.5.2”,
“multer”: “^1.4.5-lts.1”,
“nodemailer”: “^6.9.7”,
“shippo”: “^1.7.1”
},
“devDependencies”: {
“concurrently”: “^8.2.1”,
“dotenv”: “^16.4.5”,
“nodemon”: “^3.0.1”
}
}

We commonly see that devDependencies aren’t installed when a NODE_ENV is set to production in a service’e environment variables. This will signal to the build that it should only install dependencies and skip devDependencies.

Unless you have a specific reason for doing so, we recommend against setting a NODE_ENV in your service’s environment variables as we automatically set it to production at runtime, anyway.

Is that variable set in your service’s configuration on Render and is it set to production? (A .env in the repo wouldn’t override it being set on the service.)

1 Like

Is ‘that’ variable your referring to the NODE_ENV? If so, yes my NODE_ENV in the render environment is set to ‘production’. And I have not included .env in my repo. But I’ve always had NODE_ENV as a part of my render environment from the start and never had issues with it until a month ago.

update: removing NODE_ENV fixed it!

1 Like