Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Blake Baxendell
Blake Baxendell
16,344 Points

Require is not defined when it is.

Got an error in my terminal and when I went to JSHint it is stating the require is not defined but it is.

'use strict';

var mongoose = require("mongoose");

mongoose.connect("mongodb://localhost:27017/sandbox");

var db = mongoose.connection;

db.on("error", function(err){
    console.error("connection error:", err);
});

db.once("open", function(){
    console.log("Db connection success");
    //All db communication goes here

    var Schema = mongooose.Schema;
    var AnimalSchema = new Schema({
        type: String,
        color: String,
        size: String,
        mass: Number,
        name: String
    });

    var Animal = mongoose.model("Animal", AnimalSchema);

    var elephant = new Animal({
        type: "elephant",
        color: "gray",
        size: "big",
        mass: 6000,
        name: "Lawrence"
    });

    elephant.save(function(err) {
        if(err) console.error("save failed.", err);
        else console.log("saved!");
        db.close(function(){
            console.log("db connection closed");
        });
    });

});

Are you able to require anything else in this file? If not, are you using it in node? 'require' is not native to the browser and will not work without added tools like Browserify.

From the docs: 'First be sure you have MongoDB and Node.js installed.'

Would need more information pertaining your environment to help further.

Blake Baxendell
Blake Baxendell
16,344 Points

I do get:

connection error: { Error: connect ECONNREFUSED [localhost ip]:27017 at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1161:14) name: 'MongoError', message: 'connect ECONNREFUSED [localhost ip]:27017' }

I don't remember if I had this error before or not. The above is not my normal ip. So not sure where that is.