4

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.

nnnnnn
  • 147,572
  • 30
  • 200
  • 241
99Problems
  • 155
  • 1
  • 2
  • 8
  • Do you get a specific error message? (Are you able to `require()` other modules in the same project?) – nnnnnn Aug 19 '16 at 07:44
  • yes .. it says `ReferenceError: require is not defined const l = require("lodash");` .. I haven't tried yet, lodash is the first thing ever I would like to use.. but I do require stuff in my gulpfile.js, and that works – 99Problems Aug 19 '16 at 07:49
  • 1
    Are you using `require()` in server-side JS or client-side (browser) JS? – nnnnnn Aug 19 '16 at 07:53
  • I'm using it on client side. I know it is a server-thing,. so I `npm install browserify`, but can't get it how to use :/ – 99Problems Aug 19 '16 at 07:55
  • It's not a server thing. Read the [documentation](https://lodash.com/#download) and include it in the HTML file like any other JS library. – JJJ Aug 19 '16 at 07:57
  • i have red multiple times now, that require is a server thing.. by the way, when I should include it like a normal script to my HTML-file, why should I install the node-package then? – 99Problems Aug 19 '16 at 08:04
  • To use Lodash client-side, just include `` (with the appropriate path) in your page. Use `require()` when you want to use Lodash server-side. You shouldn't need to install the package in Node if you aren't using it server-side. – nnnnnn Aug 19 '16 at 08:16
  • You *shouldn't* install the Node package if you're using it client-side. Just download and include the JS file. NPM is for managing your Node libraries, not your client-side libraries. (You *can* download it with NPM if you want to use it as a package manager, but at this point it's probably better to not go that route.) – JJJ Aug 19 '16 at 08:26
  • Run the command `browserify main.js -o "/public/js/bundle.js"` where main.js is your client-side javascript where you do `require(lodash)` and the `-o` argument is your output folder, typically a folder accessible from your webpage. Browserify will bundle all javascript needed by this file to the output file, which will be downloaded later on client side. – krassdanke Aug 19 '16 at 08:46
  • @dth .. thanks dude!! :) .. that was exaxtly what I was searching for!! .,. thank you very much! :) – 99Problems Aug 19 '16 at 11:00

2 Answers2

4

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.

AndreaM16
  • 3,917
  • 4
  • 35
  • 74
0

it worked for me. I changed

"dependencies": {
    "@types/lodash": "^4.14.187", ...

to

"dependencies": {
    "lodash": "^4.14.187", ...

in the package.json file.

Mustafa Poya
  • 2,615
  • 5
  • 22
  • 36