Docker build / buildkit does not use volumes even though you define them in the compose. Volumes are for running containers only.
The entire point of the build context is that the build is repeatable and consistent and volumes during build would break that idea.
If you are trying to optimize your npm build times/sizes You could look at adding additional contexts https://docs.docker.com/reference/cli/docker/buildx/build/#build-context
Also make sure your build has network access for npm i.e add
docker build --progress=plain --network host .