admin管理员组

文章数量:1310034

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.

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.

Share Improve this question edited Aug 19, 2016 at 7:41 nnnnnn 150k30 gold badges209 silver badges247 bronze badges asked Aug 19, 2016 at 7:39 99Problems99Problems 1551 gold badge2 silver badges8 bronze badges 10
  • Do you get a specific error message? (Are you able to require() other modules in the same project?) – nnnnnn Commented Aug 19, 2016 at 7: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 Commented Aug 19, 2016 at 7:49
  • 1 Are you using require() in server-side JS or client-side (browser) JS? – nnnnnn Commented Aug 19, 2016 at 7: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 Commented Aug 19, 2016 at 7:55
  • It's not a server thing. Read the documentation and include it in the HTML file like any other JS library. – JJJ Commented Aug 19, 2016 at 7:57
 |  Show 5 more ments

2 Answers 2

Reset to default 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.

it worked for me. I changed

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

to

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

in the package.json file.

本文标签: javascriptrequire(quotlodashquot)require is not definedStack Overflow