Unable to run 'npm start' in Mac - node.js

I am trying to run the following command "npm start" however it throws the following error:
internal/net.js:17
throw new RangeError('"port" argument must be >= 0 and < 65536');
^
RangeError: "port" argument must be >= 0 and < 65536
at assertPort (internal/net.js:17:11)
at Server.listen (net.js:1389:5)
at EventEmitter.listen (/Users/keyurshah/fulljs/node_modules/express/lib/application.js:617:24)
at Object.<anonymous> (/Users/keyurshah/fulljs/server.js:10:8)
at Module._compile (module.js:570:32)
at loader (/Users/keyurshah/fulljs/node_modules/babel-register/lib/node.js:144:5)
at Object.require.extensions.(anonymous function) [as .js] (/Users/keyurshah/fulljs/node_modules/babel-register/lib/node.js:154:7)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
[nodemon] app crashed - waiting for file changes before starting...

Check server.js line 10. Your port is probably specified there.
If not just declare it to be what you want right before the listen function.
From
app.listen(port, ...)
to
app.listen(8080, ...)
Otherwise you haven't shown any code not sure how we could help you with such limited information...
I recommend you start with more simple projects and get a feeling for how things work: http://expressjs.com/en/starter/hello-world.html

try to do this or change with any port number you like .listen(portNumber)
var http = require('http');
http.createServer( function (request, response) {
//your code
}).listen(8081);

Related

How to get listener object when binding to a specific address in Express?

I am using express 4.16.2
If I only pass a port number to express
listener = app.listen(3000);
console.log(listener.address().port);
I get 3000 in the console.
If I bind to a specific address
listener = app.listen(3000, '192.168.0.10');
console.log(listener.address().port);
I get the following error
console.log(listener.address().port);
^
TypeError: Cannot read property 'port' of null
at Object.<anonymous> (D:\development\node\redbird_redis\app.js:13:31)
at Module._compile (module.js:643:30)
at Object.Module._extensions..js (module.js:654:10)
at Module.load (module.js:556:32)
at tryModuleLoad (module.js:499:12)
at Function.Module._load (module.js:491:3)
at Function.Module.runMain (module.js:684:10)
at startup (bootstrap_node.js:187:16)
at bootstrap_node.js:608:3
You must wait for the listening event to emit:
Don't call server.address() until the 'listening' event has been emitted.
The simplest and most canonical way to do that is to add a callback to the .listen() invocation:
server = app.listen(3000, '192.168.0.10', () => console.log(server.address().port));

nodemon - app crashed - waiting for file changes before starting

I just started learning Node.js. The idea of this small application is to use express and mongosse to store some users in a based cloud database (mongoDB via mlab).
I have two seperate files :
User.js (models/User.js)
var mongoose = require('mongoose');
module.exports = mongoose.model('User', {
email : string ,
pwd : string
});
server.js (dossier root)
var express = require('express')
var cors = require('cors')
var bparser = require('body-parser')
var mongoose = require('mongoose')
var User = require('./models/User.js')
var app = express()
app.use(cors())
app.use(bparser.json())
app.post('/register', (req,res) => {
userData = req.body;
var user = new User(userData);
user.save((err, result) => {
if(err) console.log('IL YA UNE ERREUR')
result.sendStatus(200);
})
})
mongoose.connect('mongodb://user:pwd#ds261755.mlab.com:61755/myapp', { useMongoClient: true } , (erreur) => {
if(!erreur)
console.log('Connexion etablie');
})
app.listen(3000)
When I execute : nodemon server.js, I get the error below:
D:\Bureau\MEAN\appBackend\models\User.js:5
email : string ,
^
ReferenceError: string is not defined
at Object.<anonymous> (D:\Bureau\MEAN\appBackend\models\User.js:5:12)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (D:\Bureau\MEAN\appBackend\server.js:6:12)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:389:7)
[nodemon] app crashed - waiting for file changes before starting...
Do you have any idea about this error?
Mongoose expects you to specify types using the built-in constructor functions, which are named with capital letters, e.g. String, Number, Boolean, etc.
var mongoose = require('mongoose');
module.exports = mongoose.model('User', {
email : String ,
pwd : String
});
define
module.exports = mongoose.model('User', new mongoose.Schema({
email : 'string' ,
pwd : 'string'
})
});
There is no string variable in the code .
this message will be appeared always when there's any small mistake.
I also faced this when module.exports type as module.export,just missed 's' only.
I come across with such error and on my case it was syntax error in the node js file.check your code for error as nodemon listen to change and run if no error there.
This was the error that make app-crashed ....
console.log("this is author",cope["author"];
SyntaxError: missing ) after argument list
at new Script (vm.js:79:7)
at createScript (vm.js:251:10)
at Object.runInThisContext (vm.js:303:10)
at Module._compile (internal/modules/cjs/loader.js:657:28)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
at startup (internal/bootstrap/node.js:283:19) After i correct the error [nodemon] restarting due to changes... [nodemon]
starting node server.js
That is it. Check for error in your code the nodemon automatically start the server.That is why nodemon is best rather than using node filename.js cz every time you have to start whenever you change the code.
The common reason is that you're trying to access the database from an IP that isn't whitelisted.
For my case, I was working on the same project on another PC. So, i had to whitelist the PC IP.
To do this, login to your cloud base mongodb, navigate to that your particular db project, and click on network access, add IP address and click on current ip address.
Also, make sure on your terminal, you run mongod to start your mongodb.
That solve for me
[nodemon] app crashed - waiting for file changes before starting...
You can fix this error with adding "start": "nodemon server.js" entry to your package.json file.
Test with this command:
npm install --save express-load
Many times we closed the PC and left the project running, when we used it again and ran the project again, that error appeared. I always solved it, restarting the pc. Everything worked perfectly.

Npm test Yeoman Custom Generator

some days ago i've started to write a custom yeoman generator and i want to make a test for it (so i can use travis ecc) but i have a problem when i lunch npm test.
My generator copy only 4 files, a gulpfile.js and 3 files each in a different folder so the final result is:
Project-folder
|
|----- gulpfile.js
|--------production
---------css/main.css
---------html/index.html
---------js/index.js
I want to test my generator so i write this test,i start to write it from the template test provided by generator-generator
app.js
'use strict';
var path = require('path');
var assert = require('yeoman-assert');
var helpers = require('yeoman-test');
describe('generator-postcss-template:app', function () {
before(function () {
return helpers.run(path.join(__dirname, '../generators/app'))
.withPrompts({someAnswer: true})
.toPromise();
});
it('creates files', function () {
assert.file([
'production/css/styles.css',
'production/html/index.html'
]);
});
});
When i lunch the npm test i have this error
module.js:471
throw err;
^
Error: Cannot find module 'gulp-eslint'
at Function.Module._resolveFilename (module.js:469:15)
at Function.Module._load (module.js:417:25)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/run/media/giulio/Lavoro/Personal/generator-postcss-template/gulpfile.js:4:14)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
npm ERR! Test failed. See above for more details.
i don't know how fix it,test of generator-generator is the same but check only if dummyfile was created and test pass with 100%
Some one can help me?
-- Update--
after deletion of all modules and reinstall it the error was changed
new error

Node.js FS module start prob

Ok so node.js has been driving me crazy. I have been trying to run the exact same code from the book. But it throwing me an error. The code is simple and watches a file for changes
const fs= require('fs');
fs.watch('target.txt', function() {
console.log("File 'target.txt' just changed!");
});
console.log("Now watching target.txt for changes");
the js file for the code and text.txt is in the same dir. And I get this error
$ node --harmony watcher.js
fs.js:1051
throw errnoException(process._errno, 'watch');
^
Error: watch ENOENT
at errnoException (fs.js:1019:11)
at FSWatcher.start (fs.js:1051:11)
at Object.fs.watch (fs.js:1076:11)
at Object.<anonymous> (C:\cygwin64\home\Sinan\try\watcher.js:5:5)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
Thanks beforehand.
I would also appropriate an uptodate source for learning node, I have tried 2 books and I can not even run example codes
I removed .txt extension and it worked.
fs.watch('target', function () {
console.log("File target.txt just changed");
});

Error when using vhost in express

My code for server.js is:
var express = require('express');
var ENV = process.env['NODE_ENV'] || 'development';
var config = require('./config')[ENV];
// The express server to listen for both the clients
var main = express();
// Main application
main.use(express.vhost('vypics', require('./web_app/app').app))
// Example sub domain
main.use(express.vhost('android.vypics',require('./android_app/app').app));
main.listen(3000);
console.log('started on 3000');
when I am running node server.js I am getting the following error.
/Users/saransh2012/Developer/vypics/node_modules/express/node_modules/connect/lib/middleware/vhost.js:30
if (!server) throw new Error('vhost server required');
^
Error: vhost server required
at Function.vhost (/Users/saransh2012/Developer/vypics/node_modules/express/node_modules/connect/lib/middleware/vhost.js:30:22)
at Object.<anonymous> (/Users/saransh2012/Developer/vypics/server.js:8:18)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:901:3
Please help.....What Am i Doing wrong.
You'll get that error when the second argument to express.vhost is falsy.
My guess would be that either require('./web_app/app').app or require('./android_app/app').app (or both) is undefined.
The error was in the require('.web_app/app').app part as mentioned by robertklep.It was a very silly error on my part as i forgot to write the export for the app.
Sorry guys for such a silly error but sometimes those are the one very difficult to see.

Resources