NPM issues deploying to heroku

I’ve been through hell and back the past week trying to get NPM to work on a backend microservice project.

A little about me - I come from iOS engineering which is a much more contained environment where I’ve never really faced any issues with dependencies. With web programming, we’re directly accessing the filesystem when installing dependencies and it can get tricky.

I’ll spare the long details - npm install wasn’t working for me because the express package had too many symbolic links (not sure how it happened because I never explicitly symlinked anything).

Long story short, I uninstalled node and npm, tried installing separately, uninstalled all over again and then installed them together. I deleted my node_modules directory and my package.json file and ran npm init all over again. It worked - I was able to use npm install as expected.

In deploying to heroku, my app wouldn’t load when I visited the URL. When I checked the heroku logs, the build said it couldn’t find TWO modules - express and ./authenticate

Express is one thing…I can’t even find information on ./authenticate. If the package is just called “authenticate”, I tried installing it locally AND globally and pushed to heroku all over again. NO LUCK.

Another word on this - I’m not using the authenticate module ANYWHERE in my project. I’ve never seen it before even when I was running my server locally. No clue where it came from - it seems to just be coming from my reinitializing my nodes modules folder and package.json file. Also, why would it show up as “cannot find module ./ authenticate” instead of just “cannot find module authenticate”?

Any and all help would be appreciated - I’ve sunk a crazy number of hours into this.

I’m not sure about the authentication module but concerning express, did you make sure to save express to your dependencies when you installed express?
Did you run npm install express --save ?

Absolutely - I save most of my dependencies globally just because I don’t trust the issues that occur all too frequently with NPM. The full error is "cannot find module ‘./authenticate’ … at Object. (/app/node_modules/mongodb/lib/db.js:4:20)

Not really sure what mongodb has to do with this - I ran the server locally and added a few objects to my DB hosted on mlab and authentication worked fine and the data was passed to the hosted DB. This is so bizarre.

Make sure you setup your start scripts, and you set the port to process.env.PORT

const port = process.env.PORT || 3000;

app.listen(port, () => {
  console.log(`Server listening on port: ${port}`);
});

Yep - I changed my port before deploying to Heroku. Not really sure what to do with a start script since I’ve been able to deploy previous projects without one - came across the topic when researching my problem but unsure if I actually need one.

Can you post your package.json file? The issue likely lies there. You need to have npm dependencies listed on the package.json file and a start script.

Sure!

{
  "name": "express.js-url-shortener",
  "version": "1.0.0",
  "description": "microservice to shorten a url",
  "main": "helperfunctions.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node server.js"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/ksatia/express.js-short-url.git"
  },
  "author": "karan satia",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/ksatia/express.js-short-url/issues"
  },
  "homepage": "https://github.com/ksatia/express.js-short-url#readme",
  "dependencies": {
    "authenticate": "*",
    "body-parser": "^1.17.2",
    "express": "^4.15.3",
    "mongodb": "*"
  }
}

I only put authenticate in there because it was listed as a module that couldn’t be found - I don’t explicitly use it anywhere in my project and am a little confused by it. It seems to be somehow related to mongodb (see error message below, the last line specifically) :

2017-06-27T21:55:13.591270+00:00 app[web.1]: Error: Cannot find module './authenticate'
2017-06-27T21:55:13.591271+00:00 app[web.1]:     at Function.Module._resolveFilename (module.js:469:15)
2017-06-27T21:55:13.591272+00:00 app[web.1]:     at Function.Module._load (module.js:417:25)
2017-06-27T21:55:13.591272+00:00 app[web.1]:     at Module.require (module.js:497:17)
2017-06-27T21:55:13.591273+00:00 app[web.1]:     at require (internal/module.js:20:19)
2017-06-27T21:55:13.591274+00:00 app[web.1]:     at Object.<anonymous> (/app/node_modules/mongodb/lib/db.js:4:20)

I would switch the authenticate and mongodb dependencies to a specific version instead of “*”

Also, do you need a main and a start script? I never include main. I might remove main": “helperfunctions.js” and just make sure you require this file on your server.js.