Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yarn install production dependencies of a single package in workspace

I'm trying to install the production dependencies only for a single package in my workspace. Is that possible?

I've already tried this:

yarn workspace my-package-in-workspace install -- --prod

But it is installing all production dependencies of all my packages.

like image 612
Luiz Guilherme Avatar asked Dec 20 '25 14:12

Luiz Guilherme


1 Answers

yarn 1 doesn't support it as far as I know.

If you are trying to install a specific package in a dockerfile, then there is a workaround:

  1. copy the yarn.lock file and the root package.json

  2. copy only the packages's package.json that you need: your package and which other packages that your package depends on (locally in the monorepo).

  3. in the dockerfile, manually remove all the devDependnecies of all the package.json(s) that you copied.

  4. run yarn install on the root package.json.

Note:

  • Deterministic installation - It is recommended to do so in monorepos to force deterministic install - https://stackoverflow.com/a/64503207/806963

Full dockefile example:

FROM node:12

WORKDIR /usr/project

COPY yarn.lock package.json remove-all-dev-deps-from-all-package-jsons.js change-version.js ./

ARG package_path=packages/dancer-placing-manager

COPY ${package_path}/package.json ./${package_path}/package.json

RUN node remove-all-dev-deps-from-all-package-jsons.js && rm remove-all-dev-deps-from-all-package-jsons.js

RUN yarn install --frozen-lockfile --production

COPY ${package_path}/dist/src ./${package_path}/dist/src
COPY ${package_path}/src ./${package_path}/src

CMD node --unhandled-rejections=strict ./packages/dancer-placing-manager/dist/src/index.js

remove-all-dev-deps-from-all-package-jsons.js:

const fs = require('fs')
const path = require('path')
const { execSync } = require('child_process')

async function deleteDevDeps(packageJsonPath) {
  const packageJson = require(packageJsonPath)
  delete packageJson.devDependencies
  await new Promise((res, rej) =>
    fs.writeFile(packageJsonPath, JSON.stringify(packageJson, null, 2), 'utf-8', error => (error ? rej(error) : res())),
  )
}

function getSubPackagesPaths(repoPath) {
  const result = execSync(`yarn workspaces --json info`).toString()
  const workspacesInfo = JSON.parse(JSON.parse(result).data)
  return Object.values(workspacesInfo)
    .map(workspaceInfo => workspaceInfo.location)
    .map(packagePath => path.join(repoPath, packagePath, 'package.json'))
}

async function main() {
  const repoPath = __dirname
  const packageJsonPath = path.join(repoPath, 'package.json')
  await deleteDevDeps(packageJsonPath)
  await Promise.all(getSubPackagesPaths(repoPath).map(packageJsonPath => deleteDevDeps(packageJsonPath)))
}

if (require.main === module) {
  main()
}
like image 188
Stav Alfi Avatar answered Dec 23 '25 20:12

Stav Alfi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!