Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

Error while deploying node.js application to heroku (cannot find module dotenv, and npm ERR! errno 1)

I'm trying to upload my first node.js application to heroku. Build is showing successful, but the application cannot be opened in browser. Here is the logs tail of the deployment. I'm not quite sure where the problem(s) lie(s). All localhost routes are actually working when tested with postman. I'm using express generator, so the PORT is declared in .bin/www. I'm also using .env. I'm a bit lost about where to look next. Any suggestions? Many thanks!

2021-01-11T22:10:14.214188+00:00 app[web.1]: > [email protected] start /app
2021-01-11T22:10:14.214189+00:00 app[web.1]: > node ./bin/www
2021-01-11T22:10:14.214189+00:00 app[web.1]: 
2021-01-11T22:10:14.285451+00:00 app[web.1]: internal/modules/cjs/loader.js:818
2021-01-11T22:10:14.285453+00:00 app[web.1]: throw err;
2021-01-11T22:10:14.285453+00:00 app[web.1]: ^
2021-01-11T22:10:14.285454+00:00 app[web.1]: 
2021-01-11T22:10:14.285454+00:00 app[web.1]: Error: Cannot find module 'dotenv'
2021-01-11T22:10:14.285454+00:00 app[web.1]: Require stack:
2021-01-11T22:10:14.285455+00:00 app[web.1]: - /app/app.js
2021-01-11T22:10:14.285455+00:00 app[web.1]: - /app/bin/www
2021-01-11T22:10:14.285456+00:00 app[web.1]: at Function.Module._resolveFilename (internal/modules/cjs/loader.js:815:15)
2021-01-11T22:10:14.285456+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:667:27)
2021-01-11T22:10:14.285457+00:00 app[web.1]: at Module.require (internal/modules/cjs/loader.js:887:19)
2021-01-11T22:10:14.285457+00:00 app[web.1]: at require (internal/modules/cjs/helpers.js:74:18)
2021-01-11T22:10:14.285458+00:00 app[web.1]: at Object.<anonymous> (/app/app.js:1:1)
2021-01-11T22:10:14.285458+00:00 app[web.1]: at Module._compile (internal/modules/cjs/loader.js:999:30)
2021-01-11T22:10:14.285459+00:00 app[web.1]: at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
2021-01-11T22:10:14.285459+00:00 app[web.1]: at Module.load (internal/modules/cjs/loader.js:863:32)
2021-01-11T22:10:14.285459+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:708:14)
2021-01-11T22:10:14.285460+00:00 app[web.1]: at Module.require (internal/modules/cjs/loader.js:887:19) {
2021-01-11T22:10:14.285460+00:00 app[web.1]: code: 'MODULE_NOT_FOUND',
2021-01-11T22:10:14.285461+00:00 app[web.1]: requireStack: [ '/app/app.js', '/app/bin/www' ]
2021-01-11T22:10:14.285461+00:00 app[web.1]: }
2021-01-11T22:10:14.301983+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2021-01-11T22:10:14.302357+00:00 app[web.1]: npm ERR! errno 1
2021-01-11T22:10:14.309690+00:00 app[web.1]: npm ERR! [email protected] start: `node ./bin/www`
2021-01-11T22:10:14.309804+00:00 app[web.1]: npm ERR! Exit status 1
2021-01-11T22:10:14.309965+00:00 app[web.1]: npm ERR!
2021-01-11T22:10:14.310034+00:00 app[web.1]: npm ERR! Failed at the [email protected] start script.
2021-01-11T22:10:14.310119+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2021-01-11T22:10:14.317026+00:00 app[web.1]: 
2021-01-11T22:10:14.317243+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2021-01-11T22:10:14.317339+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2021-01-11T22_10_14_310Z-debug.log
2021-01-11T22:10:14.361923+00:00 heroku[web.1]: Process exited with status 1
2021-01-11T22:10:14.435764+00:00 heroku[web.1]: State changed from starting to crashed
2021-01-11T22:10:14.439118+00:00 heroku[web.1]: State changed from crashed to starting
2021-01-11T22:10:16.805923+00:00 heroku[web.1]: Starting process with command `npm start`
2021-01-11T22:10:19.751944+00:00 app[web.1]: 
2021-01-11T22:10:19.751961+00:00 app[web.1]: > [email protected] start /app
2021-01-11T22:10:19.751962+00:00 app[web.1]: > node ./bin/www
2021-01-11T22:10:19.751963+00:00 app[web.1]: 
2021-01-11T22:10:19.808881+00:00 app[web.1]: internal/modules/cjs/loader.js:818
2021-01-11T22:10:19.808883+00:00 app[web.1]: throw err;
2021-01-11T22:10:19.808883+00:00 app[web.1]: ^
2021-01-11T22:10:19.808883+00:00 app[web.1]: 
2021-01-11T22:10:19.808884+00:00 app[web.1]: Error: Cannot find module 'dotenv'
2021-01-11T22:10:19.808884+00:00 app[web.1]: Require stack:
2021-01-11T22:10:19.808884+00:00 app[web.1]: - /app/app.js
2021-01-11T22:10:19.808885+00:00 app[web.1]: - /app/bin/www
2021-01-11T22:10:19.808885+00:00 app[web.1]: at Function.Module._resolveFilename (internal/modules/cjs/loader.js:815:15)
2021-01-11T22:10:19.808885+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:667:27)
2021-01-11T22:10:19.808886+00:00 app[web.1]: at Module.require (internal/modules/cjs/loader.js:887:19)
2021-01-11T22:10:19.808886+00:00 app[web.1]: at require (internal/modules/cjs/helpers.js:74:18)
2021-01-11T22:10:19.808887+00:00 app[web.1]: at Object.<anonymous> (/app/app.js:1:1)
2021-01-11T22:10:19.808887+00:00 app[web.1]: at Module._compile (internal/modules/cjs/loader.js:999:30)
2021-01-11T22:10:19.808887+00:00 app[web.1]: at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
2021-01-11T22:10:19.808888+00:00 app[web.1]: at Module.load (internal/modules/cjs/loader.js:863:32)
2021-01-11T22:10:19.808888+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:708:14)
2021-01-11T22:10:19.808889+00:00 app[web.1]: at Module.require (internal/modules/cjs/loader.js:887:19) {
2021-01-11T22:10:19.808889+00:00 app[web.1]: code: 'MODULE_NOT_FOUND',
2021-01-11T22:10:19.808889+00:00 app[web.1]: requireStack: [ '/app/app.js', '/app/bin/www' ]
2021-01-11T22:10:19.808890+00:00 app[web.1]: }
2021-01-11T22:10:19.834122+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2021-01-11T22:10:19.834754+00:00 app[web.1]: npm ERR! errno 1
2021-01-11T22:10:19.843644+00:00 app[web.1]: npm ERR! [email protected] start: `node ./bin/www`
2021-01-11T22:10:19.843996+00:00 app[web.1]: npm ERR! Exit status 1
2021-01-11T22:10:19.844353+00:00 app[web.1]: npm ERR!
2021-01-11T22:10:19.844619+00:00 app[web.1]: npm ERR! Failed at the [email protected] start script.
2021-01-11T22:10:19.844914+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2021-01-11T22:10:19.858827+00:00 app[web.1]: 
2021-01-11T22:10:19.859298+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2021-01-11T22:10:19.859536+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2021-01-11T22_10_19_845Z-debug.log
2021-01-11T22:10:19.946324+00:00 heroku[web.1]: Process exited with status 1
2021-01-11T22:10:20.030800+00:00 heroku[web.1]: State changed from starting to crashed
2021-01-11T22:10:22.579029+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=XXXXXX.herokuapp.com request_id=28ed0f16-48b7-4edb-a4d4-bf5af57518a5 fwd="91.106.154.160" dyno= connect= service= status=503 bytes= protocol=https
2021-01-11T22:10:23.309377+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=XXXXXX.herokuapp.com request_id=967010fc-984e-49c7-a73e-9d9758907f49 fwd="91.106.154.160" dyno= connect= service= status=503 bytes= protocol=https

My package json looks like this:

{
  "name": "finale-backend",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "start": "node ./bin/www",
    "dev": "nodemon ./bin/www"
  },
  "dependencies": {
    "bcrypt": "^5.0.0",
    "body-parser": "^1.19.0",
    "cookie-parser": "~1.4.4",
    "cors": "^2.8.5",
    "debug": "~2.6.9",
    "express": "~4.16.1",
    "helmet": "^4.3.1",
    "jsonwebtoken": "^8.5.1",
    "logger": "0.0.1",
    "morgan": "~1.9.1",
    "nodemon": "^2.0.7",
    "pg": "^8.5.1"
  },
  "devDependencies": {
    "dotenv": "^8.2.0"
  },
  "engines": {
    "node": "12.x"
  }
}

And I require dotenv in my entry point app.js like this

require('dotenv').config();
const express = require('express');
const path = require('path');
const cookieParser = require('cookie-parser');
const logger = require('morgan');
const cors = require('cors') 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It seems like Heroku is not installing devDependencies by default. You may want to move dotenv to dependencies in your package.json and try again.

If you don't need dotenv in your production environment, you may want to remove the require altogether and use a different command to run your development server, as described here:

https://www.npmjs.com/package/dotenv#preload

node -r dotenv/config your_script.js

This will prevent your server from crashing in production when the dotenv dependency is not found on Heroku.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...