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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With