DEV Community

Cover image for Forces Yarn to be used on a project by using "only-allow"
suin
suin

Posted on

4 1

Forces Yarn to be used on a project by using "only-allow"

only-allow is a tool that enforces developers to use a specific package manager on their project.

Problems

The standard package manager for JavaScript is NPM, however there are other popular package managers such as Yarn and PNPM.

Between the package managers, there are some incompatibilities. For example, npm and yarn lock the versions of the installed packages in a different way. NPM saves it in package-lock.json, Yarn does in yarn.lock.

To use the same one package manager among the developers is important. This is to prevent unexpected incompatibilities.

Solutions

Documentation

The simplest way to employ the one same package manager in the team is to call a developer's attention by documentation. For example, you can write about it on README.md like:

"Please use Yarn to set up this project!"

However, this way sometimes fails. Some developers don't read it. There is no force in documentation.

"only-allow"

The better way to enforce the developers to use a specific package manager is to employ only-allow. It enforces developers to use a specific package manager on their project.

If a developer tries to use NPM in a Yarn-only-allowed project, the process will fail. only-allow will tell s/he to use Yarn instead of NPM:

How to set up "only-allow"

To set up only-allow is quite simple. Just adds a preinstall script to the package.json:

{
  "scripts": {
    "preinstall": "npx only-allow yarn"
  }
}
Enter fullscreen mode Exit fullscreen mode

It does't need to install only-allow.

Conclusion

  • To use a same package manager among developers in a project is important.
  • Documentation sometimes can't force the developers to use the same package manager.
  • only-allow is useful to force to use the same package manager in a project.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay