Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

require("lodash") - require is not defined

I saw this lodash-thing once and thought it would be nice to use. So I try now for days, but can't get it running. Please be patient as I'm a noob in this thing.

I have npm install lodash --save .. so it is in my node_modules and in my package.json I have:

"dependencies": {
"del": "^2.2.2",
"gulp": "^3.9.1",
"gulp-concat": "^2.6.0",
"gulp-notify": "^2.2.0",
"gulp-sass": "^2.3.2",
"gulp-uglify": "^2.0.0",
"lodash": "^4.15.0"
},

so now, when I do const _ = require("lodash"); it should work, right? If not, what did I forgot?

would be nice if someone could explain it to me. I'm watching and reading tutorials for days now, but whatever I try, I can't get it working.

like image 849
99Problems Avatar asked Oct 19 '25 14:10

99Problems


1 Answers

You should get it going with adding lodash in your package.json and then doing npm install

{
  "name" : "myApp",
  "main" : "server.js",
  "dependencies": {
    . . .
    "lodash"          : "~4.15.0"
  }
}

And doing the following in your server.js

. . .
var _ = require('lodash');
. . .
//When doing ._ calls lodash, defined globally
global._ = _;
. . .

And then, in any .js file in your back-end

var array = [];
//Checks if an Array or an Object is Empty
if(_.isEmpty(array)){
   doSomething();
}

Or, if you do not want to use it globally, just do a require of lodash where you need it

var _. = require('lodash');

If you want to use lodash in your front-end, supposing you are using bower, you need to include lodash in your bower.json, do bower install and including lodash.js in your index by hand or using a tool to inject it like Gulp or Grunt.

I hope I've been helpful.

like image 76
AndreaM16 Avatar answered Oct 22 '25 04:10

AndreaM16



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!