ReferenceError: Require is not defined (Webstorm)

2019-02-25 21:30发布

问题:

Trying to do something incredibly basic, but running into a snag.

var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/jetbrains');

var Product = mongoose.model('Product', {name: String});

var product = new Product({name: "WebStorm"});

product.save(function(err) {
    if(err){
        console.log('failed');
    }else{
        console.log('saved');
    }
});

Continually getting ReferenceError: require is not defined at [file path].

I'm doing this in Webstorm 10. Trying to folow this tutorial on MEAN development in Webstorm. https://www.youtube.com/watch?v=JnMvok0Yks8

Ideas?

回答1:

You are probably trying to run the above code in the Browser. It's supposed to run on the Node server. Node automatically defines the require function so you would never have this problem.

Check out this question for good "getting started" material.

EDIT

That's really strange. WebStorm is definitely running Node.js if it's running on the Terminal. Can you please put a break point on the first line, debug the file and inspect the require function?