简体   繁体   中英

Heroku Deploy Error: Cannot find module '/app/index.js'

I' m trying to deploy mt app on Heroku but I always get the same error:

2016-08-18T10:16:10.988982+00:00 heroku[web.1]: Starting process with command `node index.js`
2016-08-18T10:16:13.180369+00:00 app[web.1]: module.js:341
2016-08-18T10:16:13.180389+00:00 app[web.1]:     throw err;
2016-08-18T10:16:13.180390+00:00 app[web.1]:     ^
2016-08-18T10:16:13.180391+00:00 app[web.1]: 
2016-08-18T10:16:13.180392+00:00 app[web.1]: Error: Cannot find module '/app/index.js'
2016-08-18T10:16:13.180393+00:00 app[web.1]:     at Function.Module._resolveFilename (module.js:339:15)
2016-08-18T10:16:13.180394+00:00 app[web.1]:     at Function.Module._load (module.js:290:25)
2016-08-18T10:16:13.180394+00:00 app[web.1]:     at Function.Module.runMain (module.js:447:10)
2016-08-18T10:16:13.180399+00:00 app[web.1]:     at node.js:405:3
2016-08-18T10:16:13.271966+00:00 heroku[web.1]: Process exited with status 1
2016-08-18T10:16:13.273383+00:00 heroku[web.1]: State changed from starting to crashed

As I read in similar requests I have already added a Procfile containing the following code: web: node index.js , but I still have same issue.

Anybody have any idea where the problem is? Any guidance would be greatly appreciated. Thank you in advance!

  1. Is your index.js file in your root directory?
    web: node ./index.js
  1. Your file might be nested like so app/src/index.js
    web: node ./src/index.js
  1. Does your index.js have an uppercase 'I'? It has to be index.js and not Index.js

If you do have your index.js file at the root of your project, but heroku's error says that the module cannot be found. Then, the problem you are having might be due to GIT .

How can we make sure this is the case? Well, your git repo might not be adding your index.js file to commits nor pushing it to heroku. You can verify this by looking at the files that git is watching in your local repo with the following command:

git ls-files

Your index.js file should be listed. If not, then your file is being ignored.

Solution : Force add your file.

git add --force ./index.js

Now you can commit and push to heroku and you should be up and running.

This might also be the case when having your index file inside a dist directory or src (app/dist/index.js or app/src/index.js).

为 index.js 文件添加相对路径,如下所示

web: node ./index.js

My issue was a fault with my local development environment. Somehow...JavaScript code was being injected into my app project, which I didn't do.

Not knowing this was occurring, Heroku started seeing errors during my push, like "can't find jQuery, even though your using it in your app". I was like "what????, I'm not even using jQuery." I then re-opened a .js file in my project...and there it was, a const variable declaring jquery. So I say all that say, check your VSCode extensions, your third party npm packages, and everything for that matter to make sure things like this are not happening to you.

如果错误仍然发生,请尝试使用:

web: node .

Go to your package.json file in root directory of any sub directory with another instance.

Change the script tag from "start": "node start" to "start": "node app.js" .

Please note: in my case my main file is app.js yours can be index.js or server.js or anything you named it.

I am getting this error also. then I solve it by fixing importing file. My folder was was in lowercase like the product, and I import it like Product. It doesn't give me errors in localhost. Please check if the import files path is right in the index file.

it can also happen if you give space in folders name or create your files in multiple folders then it will work in localhost but in server it will give you application error so don't make multiple folders and as well as don't give space in them and specify path correctly then it will work fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM