2

This works without lodash installed as dependency:

const _ = require('lodash');
_.each([1,2,3],console.log);

(no, I have no lodash installed globally)

I saw somewhere something like nodejs supports lodash by default, but now I cannot find any documentation about this. Is it finally true? Where can I read about it?

P.S. Finally I found and deleted node_modules in my home directory, and all the magic gone, now this script produces an error for missing dependency. Thank you guys for help in this investigation.

Yuri Gor
  • 1,353
  • 12
  • 26

3 Answers3

4

Not really. It does not make much sense to bundle out of the box 100K+ lib and assume that it would be used by the developer. For example consider this Repl example

It would do a node environment and would install any packages specified in the require statements. If none are provided it is just a plain node with nothing else.

As you can see out of the box you would get _ is not defined. but the moment you add the const _ = require('lodash') it would auto-install the lodash for you and you will get the desired result. So this is done by Repl tool for convenience so you do not have to do npm install ... etc. But out of the box node is not packaged with lodash.

Also with ES6 good amount of the lodash use cases are not there anymore and once ES6 becomes widely supported the argument about browser compatibility of lodash would also not matter as much. So going forward would make even less sense to bundle it with node.

What is interesting however is that the npm included as deps in node repo has dependencies on lodash :).

Akrion
  • 18,117
  • 1
  • 34
  • 54
  • 1
    Is there any doc about this autoinstall feature? What packages I can or cannot use such way? Will it be supported in the future? Is it safe to not list lodash in the package.json? Especially in case of developing another package which depends on lodash. – Yuri Gor Dec 02 '18 at 19:13
  • 1
    You should explicitly state your dependencies in your module. Do not rely on assumptions that `lodash is/would be/should be` part of node. – Akrion Dec 02 '18 at 21:11
  • I'm too keen to hear about this "auto install" feature - I've never heard of it – Chris Dec 03 '18 at 05:21
  • 1
    @Chris I think I probably did not do a good job with my answer if it related that there is an `auto-install` "feature". The one that exists in the `Repl tool` does the auto install since it detects the dependencies but it is an web env. so it does it for convenience. Out of the box there is no lodash with node is my contention. – Akrion Dec 03 '18 at 05:24
3

The library is not built into node. You can look at the full list of built in modules @

https://github.com/nodejs/node/tree/master/lib

dsalcovs
  • 41
  • 2
3

I think Akrion is partially correct in what he is saying (not needing lodash anymore, lodash not being part of node by default).

My guess as to why its working, is that you have a required library that in turn has a dependency on lodash and whatever bundler you are using is picking it out that way.

I would hazard a guess here to say that if you open your node_modules folder lodash is sitting there. Take a squizz through your package.lock file (or yarn lock file) and see what is including lodash.

EDIT As discovered through the comments, there was a node_modules folder in a home directory

Chris
  • 54,599
  • 30
  • 149
  • 186
  • I just created test script outside of any project, no node_modules there, but now I suspect if there is some in one of the parents. Need to investigate. – Yuri Gor Dec 03 '18 at 06:43
  • 1
    In the test script outside of any project - are you saying you can still import lodash?? If so, are 100% sure you don't have it installed globally? – Chris Dec 03 '18 at 07:00
  • Yes, script from my question just works, without any npm frictions. But now see node_modules in my home directory, and there is lodash installed, so I probably installed it some time ago by mistake. I deleted it and all the lodash magic gone. – Yuri Gor Dec 03 '18 at 07:06
  • Haha, well isn't that a head scratcher! – Chris Dec 03 '18 at 07:25
  • I wrote a package with missing lodash dep, that already was installed several hundreds times - nobody complained about missing dependency. It's too early to say lodash not needed anymore. – Yuri Gor Dec 03 '18 at 07:45
  • I don't disagree - the usage of lodash really depends on your project, your intended support, your build tools, etc. – Chris Dec 03 '18 at 10:14