Error running grunt/starting server.js - node.js

For some quick background, I am following a MEANio tutorial found here. Now, I am at the part where I have run mean init, it has finished, and then cd into the project directory and run
npm install
After fixing a brief JSON syntax issue inside package.json (the end of the file was slightly mis-typed), I attempted to run
grunt
Hook task, clean, jshint, and csslint all ran fine, and then when I get to the portion of the build script that runs
node --debug server.js
I run into errors
events.js:72
throw er; // Unhandled 'error' event
Error: listen EADDRNOTAVAIL
at errnoException (net.js:905:11)
at Server._listen2 (net.js:1024:19)
at listen (net.js:1065:10)
at net.js:1147:9
at asyncCallback (dns.js:68:16)
at Object.onanswer [as oncomplete] (dns.js:121:9)
[nodemon] app crashed - waiting for file changes before starting...
I am pretty clueless about the inner workings of Node, does anyone know what this error refers to? I followed the MEANio tutorial nearly exactly, with the exception of where my node is at, I just have it in a folder that I have added to my $PATH in my .bashrc file. Could someone give me some advice on what files to check/debug?

The EADDRNOTAVAIL error typically occurs when you are trying to run a server on a port that is already in use. That tutorial suggests that the port would be 3000 (which is a typical default for Node applications). You should check to see that you do not have anything else running on port 3000 already.

Related

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!
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.

NPM/Node not binding to port... (I believe)

I seriously have a big issue with this one guys...
I am working with StackEdit and it's code. What I am doing with it I shall not waste your time on.
Using gulp I am able to start the project easily, and build it as the original developers intended. However I am failing to build it and when I am running it via
npm start
I get the same response consistently every time. I run the command that the developers recommend using that binds the application to port 80 and then starts the node javascript application on the port. To me it seems as if its not binding to port or the port is taken by something. I have yet to try a port-avalablity check but I don't think it is nessesary considering that I can get Express applications to bind to the port 3000 (witch I have tried, by the way) appropriatley.
The command I am executing is:
(set PORT=80 && node server.js)
The following output is copied directly from the console.
Error: bind EADDRINUSE null:3000
at Object.exports._errnoException (util.js:870:11)
at exports._exceptionWithHostPort (util.js:893:20)
at cb (net.js:1296:16)
at shared (cluster.js:602:5)
at Worker.<anonymous> (cluster.js:576:9)
at process.<anonymous> (cluster.js:737:8)
at emitTwo (events.js:92:20)
at process.emit (events.js:172:7)
at handleMessage (internal/child_process.js:695:10)
at internal/child_process.js:497:7
Worker died. Spawning a new process...
events.js:141
throw er; // Unhandled 'error' event
^
It continues to repeat this cycle, printing this on a loop until I CTRL+C and terminate the job.
Again, this seems to me as if its not binding to the port correctly. Using a windows computer. If someone could help, it would be greatly appreciated!
Fixed! It turns out this was my fault. Because I am more farmilliar with bash I installed it and was using it in place of windows command prompt. This is where it failed. Hard. It ceased to operate properly. So I opened it up using the following process:
The windows key and R. Typed the command "cmd" and entered the command in the command prompt
set PORT=80 && set DEBUG=stackedit:* && node server.js
after, of course I had CD'd into the appropriate directory.

Unable to put Sails.js app in production

I'm trying to put my app into production with Sails.js, but cannot get past the grunt tasks. This is the error I'm receiving:
error: Error: The hook `grunt` is taking too long to load.
Make sure it is triggering its `initialize()` callback, or else set
`sails.config.grunt._hookTimeout to a higher value (currently 20000)
at tooLong [as _onTimeout]
(/usr/local/lib/node_modules/sails/lib/app/private/loadHooks.js:92:21)
at Timer.listOnTimeout (timers.js:110:15)
I have increased sails.config.grunt._hookTimeout dramatically and still the process hasn't been completed. Running a sails debug in either production or development outputs:
Grunt :: Error: listen EADDRINUSE
at exports._errnoException (util.js:746:11)
at Agent.Server._listen2 (net.js:1156:14)
at listen (net.js:1182:10)
at Agent.Server.listen (net.js:1267:5)
at Object.start (_debugger_agent.js:20:9)
at startup (node.js:86:9)
at node.js:814:3
I find it very strange that in development mode everything works fine, but its not the case in production. The files included are pretty big, such as angular, moment and other modules. This is how the jsFilesToInject looks:
var jsFilesToInject = [
// Load sails.io before everything else
'js/dependencies/sails.io.js',
'js/dependencies/angular.min.js',
'js/dependencies/moment.min.js',
'js/dependencies/angular-ui-router.min.js',
'js/dependencies/angular-sails.min.js',
'js/dependencies/angular-moment.min.js',
'js/dependencies/angular-animate.min.js',
'js/dependencies/angular-aria.min.js',
'js/dependencies/angular-material.min.js',
// All of the rest of your client-side js files
// will be injected here in no particular order.
'js/**/*.js'
];
I'm not sure what else would be causing this, any suggestions? I'm using Sails version 0.11.0
I just had this same problem and it was just that the timeout was not big enough I had to put this in my config/local.js file:
module.exports = {
hookTimeout: 120000
};
I just posted the same issue on github, and then checked out the source code. So I read through the grunt hook to understand what happens. And it turns out that in default mode the grunt hook triggers the callback right after grunt has started, but for the prod mode it is triggered only when grunt has finished all the tasks.
There is a following comment in the source code:
cb - optional, fires when the Grunt task has been started (non-production) or finished (production)
So if there is anything watching (like using watch in browserify) in prod, grunt task will never exit, and therefore grunt hook will always timeout. But even if nothing is watching, starting the grunt task takes much longer that finishing all the tasks, and this explains why we don't see the problem when not in production mode.
Since modifying the original grunt hook is not the best idea (it lives in node_modules), the best is indeed to increase (possibly dramatically) the _hookTimeout option and to make sure grunt task exits (for this it can be run separately with grunt prod).

nodemon is not restarting the server when using node-java package

When using node-java package, nodemon doesn't restart when the files change. If I remove node-java package then nodemon will restart when there are file changes.
Even the manual restart (rs) is not working when using node-java package in server. Following is the behavior.
alon
And even it throws the following:
events.js:85
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE
at exports._errnoException (util.js:746:11)
at Server._listen2 (net.js:1156:14)
at listen (net.js:1182:10)
at Server.listen (net.js:1267:5)
Since the port 4000 is being used only once in server and no where else, its behaving weird.
It seems that node-java somehow magically 'overrides' what's happening when receiving SIGUSR2 signal. In such a case, the SIGUSR2 signal (used by nodemon) to restart the app may fail terminating the app.
(Quick) Fix:
after the node-java has screwed your SIGUSR2 handling mechanism, add the following snippet of code:
process.once('SIGUSR2', function() {
process.kill(process.pid, 'SIGUSR2')
})
note that you must do this AFTER the node-java (or something which uses it, in my case it is node-tika) does its 'job' (in my case, immediately after requiring node-tika).
To be honest, I have only very little understanding, why this works and I'll be glad if someone can shed more light on this.
You can try running this command.
nodemon --signal SIGINT ./index.js

NodeJS deployment error

I've a project in NodeJS. I've built a framework based on ExpressJS, SocketIO, MongoDB and it works pretty well in local version.
I'm trying to deploy this project on a DigitalOcean Droplet (Debian) ; I've installed everything required (npm, node, mongo). It starts to launch the daemon and I got a big error.
To launch my app I use npm start with the following scripts
"scripts": {
"start": "./node_modules/.bin/supervisor DEBUG=unicorn ./bin/www"
}
Then it goes like that
Starting child process with 'node ./bin/www'
01:59:19 - info: Try to connect to MongoDB via Mongoose ...
01:59:19 - info: Loading Socket.io module ...
Option log level is not valid. Please refer to the README.
01:59:19 - info: Reboot system initialized (pre_starters)
01:59:19 - info: --------------------
01:59:19 - info: Unicorn (Black Mamba (0.0.4a))
01:59:19 - info: -> Welcome aboard !
01:59:19 - info: -> Your app is ready to use.
01:59:19 - info: --------------------
01:59:19 - info: Express server listening on port 8003
events.js:72
throw er; // Unhandled 'error' event
^
Error: ENOENT, open 'logs/app.log'
Program node ./bin/www exited with code 8
So i'm searching a solution for more than 3 hours now, people say it's usually because the port is busy, I checked all the processes and the port they use, and found nothing.
I also tried to change the port of Express and SocketIO (80, 8080, 3000, etc.) but it didn't change anything ...
Do you have any other idea i could try ? Or better a miraculous solution ? Thanks people ;)
PS : I also did a clean npm install from the server to have no problem with the packages compatibilities from my local version to the server.
The problem is this:
Error: ENOENT, open 'logs/app.log'
It means that your script is trying to open a logfile but fails. Since the error isn't getting caught, the script crashes.
My guess is that it's not being started in the directory you assume (one that has logs/ as a subdirectory). So you should either make sure that your script is started in the right directory, or you should use an absolute path to the logfile.

Resources