I'm using lerna to manage a multipackage monorepo.

It works well in my development environment, where I can be working on multiple packages that have dependencies on each other and the code keeps working.

However, I'm running into some trouble building this code for production.

Essentially, the problem is, when I compile my code, I'll get some cjs modules that have


The dependency will exist as a symlink in the node_modules folder, and so works in my development environment.

However, if I'm wanting to copy node_modules onto a docker image, this won't work, as Docker just ignores symlinks.

What I would like to do, is have a command that inspects node_modules for symlinks, and if they exist, then just copy all the files to be concrete files.

Is this possible? What is the terminology?

  • Is there anything wrong with using hardlinks in your case? – FelixJN May 23 '19 at 9:40
  • Are hardlinks a specific thing? Lerna creates the symlinks for you. – user1068446 May 23 '19 at 9:51
  • A symlink just points to the other location (a file name), a hardlink points to the data on the drive itself. So deleting a file means deleting all hardlinks until the disk space is set free, while symlinks are just disabled once the target they point at is gone. On the other hand symlinks can be relative (like two dirs down, one up). @Eric Mintz' use of cp -L however seems simpler than replacing all symlinks by hardlinks. – FelixJN May 23 '19 at 12:28

You could add the dereference option to the cp command. Dereference will cause cp to copy the linked file rather than the simlink.

cp --dereference --recursive node_modules/* dest_dir/

And alternatively:

cp -L -r node_modules/* dest_dir/
  • Would it be possible to copy from and to the same directory? – user1068446 May 24 '19 at 0:41
  • 1
    cp to a temp directory, then move the files back and overwrite the links. – Eric Mintz May 24 '19 at 0:50
  • Fantastic, this works! – user1068446 May 24 '19 at 2:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.