Require is not defined in angular
I am using angularjs where I created a js file (loginCtrl.js) and I included a controller. I have defined my connection and db schema in another file (server.js) and I want to include this file in my js.
My loginCtrl.js looks like this:
test.controller('loginCtrl', function($scope, loginService){
$scope.login = function(user)
{
console.log('inside function 1');
var user = require('./server.js')
console.log('inside function');
loginService.login(user);
}
});
and my server.js files look like this:
var express = require('express');
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/userregistration');
var userSchema = {
username: String,
firstname:String,
lastname: String,
email: String,
password: String
}
var User = mongoose.model('User', userSchema, 'user');
When I run the code, it gives an error like:
ReferenceError: require is not defined
It prints the above console.log.
+3
kawade
source
to share
1 answer
require()
is a NodeJS function, your angular controller will be executed in a browser that has no inline function. If you want to replicate this behavior on the client side you should have a look at RequireJS
+7
ploutch
source
to share