yortus / require-self

Lets you require('foobar') from within foobar itself
MIT License
19 stars 5 forks source link

New other way to do the same thing directly with npm #17

Open rom1504 opened 3 years ago

rom1504 commented 3 years ago

Hi, I've been using require-self for a few years, but still found it a hack, that should be solved directly in npm. Happy to say I think I've found a way to replace this: for a module called myModule simply put "myModule": "file:.", in your dev dependencies. This creates a symlink to the parent folder, the same as what require self is doing.

I think that's a good solution for the problem require-self is solving.

yortus commented 3 years ago

Hi @rom1504. Yep that's really nice. It does create cyclic symlinks which seem to cause problems with some tools that don't expect them, which is the only downside I can see. I take it you aren't having any problems in that regard though, so maybe it's not an issue in practice for most people.

groenroos commented 3 years ago

It's good to note that the behaviour here is different between npm and yarn; npm will resolve file:. into a symlink (and solved all my problems), whereas yarn will simply copy the entire directory (see yarnpkg/yarn#8096).

This can cause problems, as the side effect is that any changes you make to the module wouldn't take effect without first completely removing the node_modules directory and running yarn install again from scratch, every time.