Search code examples
node.jslodashreferenceerror

' ReferenceError: _ is not defined ' : error is shown while use LODASH module in node.js , _.isString()


I have started learning node.js through various video tutorials.

Below code is not working, the output should be 'false' for first console.log() and 'true' second console.log().

This is my code in app.js:

console.log('Starting app');     
console.log(_.isString(true));    
console.log(_.isString('Saurabh'));

OUTPUT

in CMD :

saurabh@kumar:/var/www/html/notes-node$ nodejs app.js        
    Starting app        
    /var/www/html/notes-node/app.js:25        
    **console.log(_.isString(true));**        
                ^
    **ReferenceError: _ is not defined**        
        at Object.<anonymous> (/var/www/html/notes-node/app.js:25:13)
        at Module._compile (module.js:410:26)
        at Object.Module._extensions..js (module.js:417:10)
        at Module.load (module.js:344:32)
        at Function.Module._load (module.js:301:12)
        at Function.Module.runMain (module.js:442:10)
        at startup (node.js:136:18)
        at node.js:966:3
    saurabh@kumar:/var/www/html/notes-node$ npm -v
    3.5.2

Solution

  • You have to install lodash using npm like

    npm install --save lodash
    

    Then you have to require it in file say app.js like

    const _ = require('lodash');
    

    You must have to follow these steps to use lodash in your project