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.
In my angularjs app, i try to concat bower file because i have to many lib. I make a task for concat file, here is code
var filter = require('gulp-filter');
var mainBowerFiles = require('gulp-main-bower-files');
var dest = 'dist/scripts';
gulp.task('main-bower-files', function() {
return gulp.src('./bower.json')
.pipe(mainBowerFiles([[filter, ]options][, callback]))
.pipe(gulp.dest('dist/scripts'));
});
After i call this task in nodejs cmd, i get this error in chrome console
.pipe(mainBowerFiles([[filter, ]options][, callback]))
This is error in node cmd
.pipe(mainBowerFiles([[filter, ]options][, callback]))
^^^^^^^
SyntaxError: Unexpected identifier
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:373:25)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Liftoff.handleArguments (C:\Users\aaa\AppData\Roaming\npm\node_modules\gulp\bin\gulp.js:116:3)
at Liftoff.<anonymous> (C:\Users\aaa\AppData\Roaming\npm\node_modules\gulp\node_modules\liftoff\index.js:198:16)
at module.exports (C:\Users\aaa\AppData\Roaming\npm\node_modules\gulp\node_modules\liftoff\node_modules\flagged-respawn\index.js:17:3)
If someone know solution??? Thnx
i fix this with another library, called BOWER-FILES.
var lib = require('bower-files')();
gulp.task('bower_js', function () {
gulp.src(jsBowerFile)
.pipe(concat('bower_js.js'))
.pipe(gulp.dest('dist/scripts'))
.pipe(ngmin())
.pipe(uglify({mangle: false}))
.pipe(gulp.dest('dist/scripts'));
});
I'm working on bringing a ReactJS app over to a .NET setup. I'm trying to run an NPM script from the task runner (or anywhere within VS.) When I run my build command from the task runner, it fails due to an ES6 arrow function within one of my scripts. However, if I load up cmd.exe or powershell, navigate to the project and run it by hand, it runs perfectly.
Any idea why Visual Studio would be doing this?
The output looks something like this:
> cmd.exe /c npm run build --color=always
> ...cmd1
> ...cmd2
> mocha --reporter spec tools/testSetup.js "Scripts/app/**/*.test.js"
C:\Code\HAL\dev\Test\HALTest\HALReact\tools\testSetup.js:40
Object.keys(document.defaultView).forEach((property) => {
^
SyntaxError: Unexpected token >
at Module._compile (module.js:439:25)
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 Module.require (module.js:364:17)
at require (module.js:380:17)
at C:\Code\HAL\dev\Test\HALTest\HALReact\node_modules\mocha\lib\mocha.js:220:27
at Array.forEach (native)
at Mocha.loadFiles (C:\Code\HAL\dev\Test\HALTest\HALReact\node_modules\mocha\lib\mocha.js:217:14)
at Mocha.run (C:\Code\HAL\dev\Test\HALTest\HALReact\node_modules\mocha\lib\mocha.js:469:10)
at Object.<anonymous> (C:\Code\HAL\dev\Test\HALTest\HALReact\node_modules\mocha\bin\_mocha:404: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:906:3
The file that it errors on basically just sets up JSDOM:
process.env.NODE_ENV = 'test';
// Register babel so that it will transpile ES6 to ES5
// before our tests run.
require('babel-register')();
// Disable webpack-specific features for tests since
// Mocha doesn't know what to do with them.
require.extensions['.css'] = function () {return null;};
require.extensions['.png'] = function () {return null;};
require.extensions['.jpg'] = function () {return null;};
// Configure JSDOM and set global variables
// to simulate a browser environment for tests.
var jsdom = require('jsdom').jsdom;
var exposedProperties = ['window', 'navigator', 'document'];
global.document = jsdom('');
global.window = document.defaultView;
Object.keys(document.defaultView).forEach((property) => {
if (typeof global[property] === 'undefined') {
exposedProperties.push(property);
global[property] = document.defaultView[property];
}
});
global.navigator = {
userAgent: 'node.js'
};
documentRef = document; //eslint-disable-line no-undef
Again, I can run this from the CLI directly, and it will work. It has babel-register invoked. I'm just not sure why it wants to error here. If I swap out the arrow function with a regular function, I get other errors in my tests, so it's like the task runner is not able to find babel, or is losing some environmental setting.
I have been testing TypeScript with node, all was going well until i tried splitting these out.
Am I forced to use modules ?
I have 2 files, app.ts which has a reference path to the hellofile.tst
/// <reference path="hellofile.ts" />
var testme = new Hello()
console.log(testme.testMe());
and the hellofile.ts which contains
class Hello {
testMe():string {
return "hello";
}
}
Now running the program (i am using webstorm), I get the following error.
/usr/local/bin/node app.js
/
Users/tst/WebstormProjects/NodeJsWithTypescript/app.js:2
var testme = new Hello();
^
ReferenceError: Hello is not defined
at Object.<anonymous> (/Users/tst/WebstormProjects/NodeJsWithTypescript/app.js:2: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:906:3
Process finished with exit code 8
You don't have to use modules. You can compile with --out flag
But external modules with commonjs is highly recommended if targeting nodejs : http://m.youtube.com/watch?v=KDrWLMUY0R0
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.