Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to install yarn workspace packages without symlink?

I have a yarn workspaces project which looks something like this:

node_modules
packages
  shared
    test.js
    package.json
  client
    test.js
    package.json
  server
    test.js
    package.json
package.json
server.Dockerfile

As you can see, I have a server.Dockerfile, which builds an image of the server that I can push up to different hosting providers such as Heroku or AWS.

I copy packages and package.json into this container:

COPY packages packages
COPY package.json .

And I then install only the dependencies for the server package:

RUN cd packages/server && yarn install

All the dependencies are now in the node_modules folder, and the next thing I think of doing is to delete the packages folder to remove any unnecessary code from the docker image (e.g. the client code):

RUN rm -rf packages

The problem with this is that all the yarn workspace packages inside the node_modules folder are simply symlinks to the packages folder... so I cannot delete that folder.

  • How do I get yarn install to make a copy of the yarn workspace packages instead of creating symlinks?

  • Or, is there another way to remove all of the unused code (e.g. the client code) so that my docker image isn't bloated?

like image 403
David Callanan Avatar asked Jun 13 '19 17:06

David Callanan


2 Answers

Running yarn install in workspaces does the same thing inside any package or the root directory. It installs the modules for every package and symlinks them etc.

If you want to build a docker image for just the server you should only copy that package into the container and install that as an independent package.

If the server has a dependency on the shared lib, you could publish it to npm so it can fetch it too.

like image 170
Deminetix Avatar answered Oct 23 '22 05:10

Deminetix


You can use yarn-workspace-isolator to extract the package with its local dependencies to avoid publishing them to npm if you don't want to.

isolate-workspace -w my-package -o ~/dist/my-package

Now, as the doc saying:

You can simply run yarn install inside of ~/dist/my-package and yarn will install all dependencies as if you had not used workspaces at all without having to publish any workspace dependency.

like image 4
Pierre Maoui Avatar answered Oct 23 '22 05:10

Pierre Maoui