I have the following code which Parse has posted before on their blogs, such as this one.
var _ = require("underscore");
Parse.Cloud.beforeSave("Post", function(request, response) {
var post = request.object;
var toLowerCase = function(w) { return w.toLowerCase(); };
var words = post.get("text").split(/b/);
words = _.map(words, toLowerCase);
var stopWords = ["the", "in", "and"]
words = _.filter(words, function(w) { return w.match(/^w+$/) && ! _.contains(stopWords, w); });
var hashtags = post.get("text").match(/#.+?b/g);
hashtags = _.map(hashtags, toLowerCase);
post.set("words", words);
post.set("hashtags", hashtags);
response.success();
});
The issue is that I cannot use npm start to start my server since it crashes on the line var _ = require("underscore");
. The error is
Error: Cannot find module 'underscore'.
How could I fix this in the context of parse-server's cloud code?
Simply go to package.json in your parse-server and add underscore.js as a dependency, as seen below.
"dependencies": {
"express": "~4.11.x",
"kerberos": "~0.0.x",
"parse-server": "^2.2.15",
"parse": "~1.8.0",
"nconf": "0.8.4",
"underscore": "1.8.3"
}
Then, if running the server locally, use:
npm install
and then
npm start
and the cloud code will now run fine.