Skip to content
Advertisement

Problem deploying node app on Heroku – build successful, but returns Error: Cannot find module ‘request’

I´m still experimenting with node and this modern architecture with javascript, react and so on. But I have a lot of experience with web development (PHP, ASP.NET). Anyway, I´m working on transcoding of my old frameworks to node and trying out Heroku to host it. At the beginning stages, I had managed to deploy and make my application run completely in the web to test it out. Then, I started to work on many other parts from my application and never tested the deploy again, until now.

I already searched the full day to debug the problems that were occurring and managed to get to a point where the build in Heroku is successful. The problem is that when I run the access the address where was supposed to work, it returns me a generic error:

at=error code=H10 desc=”App crashed” method=GET path=”/system” host=syncsystem-multiplatform-v1.herokuapp.com request_id=9c7e251e-4947-45cb-928e-674148718045 fwd=”186.231.136.56″ dyno= connect= service= status=503 bytes= protocol=http

And in the “view logs” section, this is what is displayed:

    2021-04-14T21:05:29.722072+00:00 heroku[web.1]: State changed from crashed to starting
    2021-04-14T21:05:38.899481+00:00 heroku[web.1]: Starting process with command `node app`
    2021-04-14T21:05:46.776113+00:00 heroku[web.1]: Process exited with status 1
    2021-04-14T21:05:46.851815+00:00 heroku[web.1]: State changed from starting to crashed
    2021-04-14T21:05:46.551799+00:00 app[web.1]: internal/modules/cjs/loader.js:626
    2021-04-14T21:05:46.551865+00:00 app[web.1]:     throw err;
    2021-04-14T21:05:46.551866+00:00 app[web.1]:     ^
    2021-04-14T21:05:46.551866+00:00 app[web.1]: 
    2021-04-14T21:05:46.551866+00:00 app[web.1]: Error: Cannot find module 'request'
    2021-04-14T21:05:46.551867+00:00 app[web.1]: Require stack:
    2021-04-14T21:05:46.551867+00:00 app[web.1]: - /app/app.js
    2021-04-14T21:05:46.551872+00:00 app[web.1]:     at Function.Module._resolveFilename (internal/modules/cjs/loader.js:623:15)
    2021-04-14T21:05:46.551872+00:00 app[web.1]:     at Function.Module._load (internal/modules/cjs/loader.js:527:27)
    2021-04-14T21:05:46.551877+00:00 app[web.1]:     at Module.require (internal/modules/cjs/loader.js:681:19)
    2021-04-14T21:05:46.551878+00:00 app[web.1]:     at require (internal/modules/cjs/helpers.js:16:16)
    2021-04-14T21:05:46.551878+00:00 app[web.1]:     at Object.<anonymous> (/app/app.js:20:17)
    2021-04-14T21:05:46.551878+00:00 app[web.1]:     at Module._compile (internal/modules/cjs/loader.js:774:30)
    2021-04-14T21:05:46.551879+00:00 app[web.1]:     at Object.Module._extensions..js (internal/modules/cjs/loader.js:785:10)
    2021-04-14T21:05:46.551879+00:00 app[web.1]:     at Module.load (internal/modules/cjs/loader.js:641:32)
    2021-04-14T21:05:46.551879+00:00 app[web.1]:     at Function.Module._load (internal/modules/cjs/loader.js:556:12)
    2021-04-14T21:05:46.551879+00:00 app[web.1]:     at Function.Module.runMain (internal/modules/cjs/loader.js:837:10) {
    2021-04-14T21:05:46.551884+00:00 app[web.1]:   code: 'MODULE_NOT_FOUND',
    2021-04-14T21:05:46.551885+00:00 app[web.1]:   requireStack: [ '/app/app.js' ]
    2021-04-14T21:05:46.551885+00:00 app[web.1]: }

During the research I did to debug, I found these main points: 1 – In my Procfile, I´ve got this line: web: node app

2 – On my package. Json, I understand that this part is essential:

    "scripts": {
        "start": "node app"
    },

3 – On my main app.js, I´ve got the configuration done with Heroku´s server variables:

    app.listen(process.env.PORT || process.env.CONFIG_SYSTEM_PORT, ()=>{
        if(gSystemConfig.configDebug === true)
        {
            console.log(`app running on port: ${ process.env.PORT || process.env.CONFIG_SYSTEM_PORT }`);
        }
    });

4 – I´ve disabled cache on server variables by Heroku´s app settings: NODEMODULESCACHE=false

5 – I´ve set up a linked github repository to make the deploy automatic: https://github.com/jorge-mauricio/syncsystem-multiplatformv1-dev/tree/master

If anyone needs to take a look at the full source code, it´s on a github repository: https://github.com/jorge-mauricio/syncsystem-multiplatformv1-dev/tree/master

There are some react folders in it, but that´s not the main focus now. The main focus is to run the application in node (app.js, backend_node, components_node), which, by the way, is running perfectly on the local environment.

The online address where is supposed to load a login screen, is at: http://syncsystem-multiplatform-v1.herokuapp.com/system

Anyone has any ideas of what I could be missing out?

Thanks, Jorge Mauricio

Advertisement

Answer

It looks the problem is exactly what the output error says: it cannot find the module request. In your file package.json, you specify the libraries (modules) you’re using, and request seems to be missing from there.

When you do for example npm install express (or yarn add express, same thing), you add this library to your package.json file. Heroku starts by getting these dependencies from package.json, so that they then can be required.

Also, from the looks of it, the request library has been deprecated. I would suggest switching to axios which seems to fulfill the same needs.

EDIT: I see now that the module is actually listed in package-lock.json, so there shouldn’t be a problem with it. However, you can try running npm install request to add it manually, then deploying to Heroku. That way, you can see if the problem seems to lie with Heroku configs or with the module itself.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement