ChatGPT解决这个技术问题 Extra ChatGPT

What is the closest to `npm ci` in yarn

In npm, there's a ci command for installing the project with a clean state. In the documentation, it is claimed that:

It can be significantly faster than a regular npm install by skipping certain user-oriented features. It is also more strict than a regular install, which can help catch errors or inconsistencies caused by the incrementally-installed local environments of most npm users.

What is the closest equivalent of the npm ci command in yarn world? Maybe the answer is that we don't need this in yarn because its architecture is such that we don't need a special mode. Maybe the answer is to use a bunch of configuration settings. However, I'm failing to find a single complete answer to this question and I believe it would be valuable to have it.


P
Pang

I believe it's as simple as that:

yarn install --frozen-lockfile

Correct according to the documentation yarnpkg.com/en/docs/cli/install
What about yarn 2?
If you're using Yarn 2, you should use --immutable instead, as it has replaced --frozen-lockfile.
@BuZZ-dEE check my newest answer stackoverflow.com/a/69944063/2834553 😉
According to the docs, --frozen-lockfile is considered as the legacy: "For backward compatibility, we offer an alias under the name of --frozen-lockfile, but it will be removed in a later release."
V
VanTanev

Unfortunately, because of the way yarn module resolution works, just doing yarn install --frozen-lockfile is sometimes not enough. You can still be left with transitive deps that are invalid.

To truly get the same behavior as npm ci you must do:

rm -rf node_modules && yarn install --frozen-lockfile

indeed! I wonder why this answer is not the most voted one :( Sometimes by accident before optimizations we do a full copy of the project files for the Dockerfile (sadly some 'guides' to copy paste from ... ) This causes a few headaches over time... The docs to explain the npm CI make it more obvious: docs.npmjs.com/cli/v7/commands/npm-ci
@KostasKapetanakis Perhaps because people are using transient build agents that have no knowledge of previous runs?
if you as running that on CI, it probably is already installing on a fresh folder. Having a node_modules already present would mean bad practice.
K
Kutyel

For newer versions of yarn you should use:

yarn install --immutable --immutable-cache --check-cache

As stated in the official Yarn docs: 😉

If the --check-cache option is set [...] This is recommended as part of your CI workflow if you're both following the Zero-Installs model and accepting PRs from third-parties, as they'd otherwise have the ability to alter the checked-in packages before submitting them.


M
Mike LP

building off of @Crafty_Shadow's recommendation, I make it a bit more integrated.

package.json

  ...
  "scripts": {
    ...
    "preci": "rm -fr node_modules",
    "ci": "yarn install --frozen-lockfile"
  },
  ...

That doesn't work for me, it seems that the package.json "ci" definition is not visible by npm in my case, dunno why though.. (note, I do not have package-lock.json, I just want to use npm ci in a generic way for a yarn project).
When using just yarn you will not have a package-lock.json file. That is created when using npm to install. You probably need to do npm run ci
@warden That's because npm ci is a native command ci. You'll have to use npm run ci, as pointed out in the previous comment.