Heroku Deploy Error: Cannot find module '/app/index.js' - node.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!

Is your index.js file in your root directory?
web: node ./index.js
Your file might be nested like so app/src/index.js
web: node ./src/index.js
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).

Add relative path for index.js file as bellow
web: node ./index.js

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.

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.

If error still happening, try using this:
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.

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.

Related

node:internal/modules/cjs/loader:998 throw err; ^ Cannot find solution to this error

Just opened my IDE for my project on react and this weird stubborn error popped up on my nodemon server.
node:internal/modules/cjs/loader:998
throw err;
^
Error: Cannot find module 'C:\Users\Hammad\ngo_project\index.js'
at Module._resolveFilename (node:internal/modules/cjs/loader:995:15)
at Module._load (node:internal/modules/cjs/loader:841:27)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:23:47 {
code: 'MODULE_NOT_FOUND',
requireStack: []
}
Node.js v18.12.1
[nodemon] app crashed - waiting for file changes before starting...
I tried deleting and reinstalling node_modules folder and even package_lock but still the same error is popping.
You will execute the node index.js command
Want to launch a file with the name index.js
You are in the folder of your project, you have this path: C:\Users\Hammad\ngo_project
Your file is not in this folder, maybe you have a folder called "js" or some other name that contains your index.js file.
When you launch the node index.js command.
Node Js is looking for your file in the project C:\Users\Hammad\ngo_project Node Js does not go to other subcathologists, therefore it does not find your index.js file.
You need to move the index.js file to your ngo_project folder.
After that, repeat the launch of the node index.js command.
Also important!
If you have "type": "module" in package.json
You need to use in your index.js connection like this: example import fetch from 'node-fetch';
If "type": "CommonJS" is specified in the package.json file or not specified at all.
You need to use in your index.js connection like this: example const fetch = require('node-fetch');
Good luck!
Try use Yarn, not npm. I use Yarn and it help me.
Delete node_modules and package-lock.json, after that in terminal write yarn install

NestJS microservices "Cannot find module"

So, I'm trying to create my first microservice using NestJS, but the moment I try to run it, the service stops with this error:
[13:39:21] Found 0 errors. Watching for file changes.
Error: Cannot find module 'C:\Users\voryi\IdeaProjects\YWA\des_server\services\learning-service\dist\main'
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
at Function.Module._load (node:internal/modules/cjs/loader:778:27)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
at node:internal/main/run_main_module:17:47
there's no main.js file in the first level of your dist directory. You can define the entry file by adding this to your nest-cli.json:
"entryFile": "learning-service/src/main"
the default is main
try npm run build and then restart your service
I use monorepo setup and this issue happens whenever I start the nestjs server from non-root folder by mistake.
Usually, removing the dist folder and restarting the nestjs server works for me.
In my case, the main.js file was inside src folder so I had to change the script to:
"start:prod": "node dist/src/main"
maybe simply use this inside the project,
npm i #nestjs/microservices

MERN deployment to Heroku

I'm having a really hard time figuring out how to deploy my app to heroku. Im new to all of this so I've tried different ways and following different tutorials but I just can't seem to find out how to do it.
This is my github repo:
https://github.com/solecity/card-deck-manager-app/tree/main
And in my logs from heroku I'm getting different errors:
2021-07-14T12:22:33.592361+00:00 app[web.1]: npm ERR! missing script: start
Starting process with command `node index.js`
2021-07-14T12:25:08.838023+00:00 app[web.1]: internal/modules/cjs/loader.js:905
2021-07-14T12:25:08.838048+00:00 app[web.1]: throw err;
2021-07-14T12:25:08.838048+00:00 app[web.1]: ^
2021-07-14T12:25:08.838049+00:00 app[web.1]:
2021-07-14T12:25:08.838049+00:00 app[web.1]: Error: Cannot find module '/app/index.js'
2021-07-14T12:27:16.812794+00:00 app[web.1]: Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'express' imported from /app/server/index.js
If anyone could shed some light on how I can fix this it will be greatly appreciated
as the error states, you need to provide a start script in your package.json
apparently this is what heroku tries to runs. (error 1)
And if it doesn't find it, it runs something like node index.js (error 2)
also, you have not registered express in your dependencies in the main package.json so heroku doesn't install it. (error 3)
I'm not very used to heroku, but i think you should create 2 separate projects, and register them separately in heroku. that way he would find onlythe appropriate package.json and run.

npm run start won't find node_modules folder on different OS aside Windows

I made a node JS application using Hapi on Windows 10. After testing it locally, the script start would run without any problem. here is the start script inside the package.json
"scripts": {
"start": "nodemon -e * ./src/server.js"
}
I am trying to deploy this app inside of Centos 7. After cloning from github, set up the postgreSQL DB & Table, set up the .env file, when I run the npm run start command, the console would pop up an error like so (error taken from Ubuntu instance on AWS, but the error are the same across every linux based OS)
[nodemon] starting `node node_modules nodemon.json package-lock.json package.json readme.MD src views /src/server.js`
internal/modules/cjs/loader.js:1032
throw err;
^
Error: Cannot find module '/home/ubuntu/application_nodejs/node_modules'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:1029:15)
at Function.Module._load (internal/modules/cjs/loader.js:898:27)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
at internal/main/run_main_module.js:17:47 {
code: 'MODULE_NOT_FOUND',
requireStack: []
}
[nodemon] app crashed - waiting for file changes before starting...
I tried to replicate the issue on Heroku, AWS EC2 running on Ubuntu & AMI 2, and my other Windows 10 PC. on Windows 10, the app would run with ease after running said command. On every linux based OS, it would return the same error. When I tried to run it with node ./src/server.js, the app would launch.
Is there any solution to my problem? I suspect it has something to do with file/folder naming or the start script itself. But I haven't found any correct solution. Thank you in advance!
You need to quote the *: nodemon -e "*" src/server.js.
Unlike Windows' cmd, Linux shells expand wildcards (as you can see in the command actually run, above the error). In Windows it's up to the program you are calling to expand wildcards. Since that is what you want in case of nodemon, it worked "by chance" on Windows without escaping the asterisk because it doesn't have any special meaning to cmd, but in Linux it will get expanded and that's not what you want.

NodeJS App 'Module Not Found' when run from Docker

I'm currently in the process of containerizing my App. Part of the App is a NodeJS Express Backend.
It works without any issues when I run it on my console with node index.js or nodemon index.js.
Inside the middlewares/index.js I do import authJwt.js and verifySignup.js via require('./verifySignup) and require('./authJwt).
This works unsurprisingly well when I run my app from the console. However If i build my docker-image using the following Dockerfile-Code:
# build environment
FROM node:14
# Working Directoy
WORKDIR /app
ENV FILE_UPLOADS="./files/uploads"
ENV FILE_UPLOADS_PART="./files/uploads_part"
ENV PORT=3001
ENV DB_HOST="localhost"
ENV DB_USER="root"
ENV DB_PASSDWORD="root_password"
ENV DB_NAME="upload_db"
ENV SECRET_KEY="bezkoder-secret-key"
# Copy Needed Files for Dependencies
COPY package.json /app/package.json
COPY yarn.lock /app/yarn.lock
# Install with YARN
RUN yarn install
# Copy Source Files
COPY ./ /app
EXPOSE 3001
CMD [ "yarn", "start" ]
When I run the Image it immediately terminates. Looking at the logs I find the following error:
$ docker logs 031241ce227a
yarn run v1.22.5
$ node index.js
internal/modules/cjs/loader.js:895
throw err;
^
Error: Cannot find module './verifySignUp'
Require stack:
- /app/middleware/middleware.js
- /app/routes/auth.routes.js
- /app/index.js
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:892:15)
at Function.Module._load (internal/modules/cjs/loader.js:742:27)
at Module.require (internal/modules/cjs/loader.js:964:19)
at require (internal/modules/cjs/helpers.js:88:18)
at Object.<anonymous> (/app/middleware/middleware.js:2:22)
at Module._compile (internal/modules/cjs/loader.js:1075:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1096:10)
at Module.load (internal/modules/cjs/loader.js:940:32)
at Function.Module._load (internal/modules/cjs/loader.js:781:14)
at Module.require (internal/modules/cjs/loader.js:964:19) {
code: 'MODULE_NOT_FOUND',
requireStack: [
'/app/middleware/middleware.js',
'/app/routes/auth.routes.js',
'/app/index.js'
]
}
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
I then exported my docker-image using docker export adoring_kowalevski > contents.tar to look at the filesystem inside the image, but everything is where it should be.
In a desperate attempt I hoped, that the issue might occur because I have multiple index.js in different folders, so I changed the names of all except the one in the root directory, but issue persisted. This is why in the error-log there is a middleware/middleware.js, it was after renaming the middleware/index.js.
Edit:
I tried using rfr to change my references from require('../middleware') to rfr('middleware'). It still workds from console but still not inside the container. So it seems that the issue doesn't occur because of the relative file paths.
Turns out I'm stupid (somewhat).
The import inside middleware/index.js looked like this require('./verifySignUp').
If we look at the file verifySignup.js inside the middleware Folder we can see, that it is named without the capital 'U'.
Why did it still work from console?
Inside verifySignup.js is the following piece of code:
module.exports = {
authJwt,
verifySignUp
};
Here it is written with capital 'U'. This means that running node index.js from the console only cared about whatever was written in the module.exports and not the actual Filename, however when running inside Docker, the Filename matters.
Renaming the file from verifySignup.js to verifySignUp.js solved my problems.

Resources