Code Monkey home page Code Monkey logo

yarn-plugin-engines's Introduction

yarn-plugin-engines

Yarn Berry plugin to enforce Node version.

Installation

$ yarn plugin import https://raw.githubusercontent.com/devoto13/yarn-plugin-engines/main/bundles/%40yarnpkg/plugin-engines.js

Usage

Specify the desired Node version constraint using engines.node field and the plugin will fail installation if the current Node version does not satisfy it:

$ cat package.json
{
    "name": "my-package",
    "engines": {
        "node": ">= 42"
    }
}
$ yarn
yarn
➤ YN0000: ┌ Project validation
➤ YN0000: │ The current node version 14.17.3 does not satisfy the required version >= 42.
➤ YN0000: └ Completed
➤ YN0000: Failed with errors in 0s 5ms

As well as fail a script execution with a non-zero exit code:

$ cat package.json
{
    "name": "my-package",
    "scripts": {
        "build": "echo success!"
    },
    "engines": {
        "node": ">= 42"
    }
}
$ yarn build
The current node version 14.17.3 does not satisfy the required version >= 42.

Supported engines keys

  • node to control the version of Node. Besides the desired version, it is possible to set this key to .nvmrc or .node-version constant. In this case plugin will read the desired version from the .nvmrc file or .node-version file.
  • yarn to control the version of Yarn.

Disable the check

Sometimes it is desired to ignore the engines check in certain environment. For example, when running renovate which uses its own Node version not controlled by the project developer. In such cases PLUGIN_YARN_ENGINES_DISABLE=1 environment variable can be set to disable the plugin.

Motivation

It's important to ensure that all developers on the team use the same version of Node to avoid "it does not work on my machine" kind of problems. With Yarn 1 one can set engines.node field in the project's package.json to communicate the required Node version, but this feature is missing in Yarn 2+. Hopefully one day this will be implemented in Yarn core making the plugin obsolete.

yarn-plugin-engines's People

Contributors

devoto13 avatar romdej avatar teriu avatar yorch avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

yarn-plugin-engines's Issues

`[email protected]` is not within ranage for `>=1.22.0`

Hi @devoto13! 👋 Thanks for this handy plugin! 💯

I just bumped Yarn from v3 to v4 RC using yarn set version canary:

-  "packageManager": "[email protected]",
+  "packageManager": "[email protected]",
   "engines": {
     "node": "^16.14.0 || ^18.0.0",
     "yarn": ">=1.22.0"
   }

Running yarn install produced this:

➤ YN0000: ┌ Project validation
➤ YN0000: │ The current Yarn version 4.0.0-rc.26 does not satisfy the required version >=1.22.0.
➤ YN0000: └ Completed
➤ YN0000: Failed with errors in 0s 3ms

Replacing "yarn": ">=1.22.0" with "yarn": ">=1.22.0 || ^4.0.0-rc" works but I wonder if this is expected behavior. WDYT?

Allow ignoring check

Engines are very useful in big companies with a lot of developers. We can make engines pretty strict, for example, allow only one exact version

{
  "engines": {
    "node": "16.10.0"
  }
}

But sometimes we can't control the environment. For example within renovate which uses 14.19.1 right now. It would be nice if we can ignore engine checks sometimes. For example with the environment variable.

Not working on Windows

Hey @devoto13, thanks for the plugin!

It looks like the plugin is not working on Windows though. It cannot find the package.json file because it seems to be duplicating the drive letter part of the path (C:)

Full stack trace:

PS C:\Repos\orkestra\orkestra-web> yarn install
➤ YN0000: ┌ Project validation
➤ YN0001: │ Error: ENOENT: no such file or directory, open 'C:\C:\Repos\orkestra\orkestra-web\package.json'
    at Object.openSync (node:fs:585:3)
    at readFileSync (node:fs:453:35)
    at validateProject (C:\Repos\orkestra\orkestra-web\.yarn\plugins\@yarnpkg\plugin-engines.cjs:6:884)
    at co.triggerHook (C:\Repos\orkestra\orkestra-web\.yarn\releases\yarn-3.0.2.cjs:295:2995)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async C:\Repos\orkestra\orkestra-web\.yarn\releases\yarn-3.0.2.cjs:306:1509
    at async xe.startTimerPromise (C:\Repos\orkestra\orkestra-web\.yarn\releases\yarn-3.0.2.cjs:275:3730)
    at async Fe.install (C:\Repos\orkestra\orkestra-web\.yarn\releases\yarn-3.0.2.cjs:306:1428)
    at async C:\Repos\orkestra\orkestra-web\.yarn\releases\yarn-3.0.2.cjs:348:14731
    at async Function.start (C:\Repos\orkestra\orkestra-web\.yarn\releases\yarn-3.0.2.cjs:275:2287)
➤ YN0000: └ Completed
➤ YN0000: Failed with errors in 0s 7ms

I hope this helps!

Cheers 🙂

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.