103

I tried to specify the node engine in a package.json to accept both 8 and 10 version.

I tried to type this:

"engines": {
  "node": "8.x|10.x"
},

But running yarn results in:

The engine "node" is incompatible with this module. Expected version "8.x|10.x"

If I replace with:

"engines": {
  "node": "10.x"
},

... it works (i.e no error).

Is there a way to accept two versions of node engine in a package.json?

tk421
  • 5,775
  • 6
  • 23
  • 34
rap-2-h
  • 30,204
  • 37
  • 167
  • 263

2 Answers2

153

You just need the double pipe || instead of a single.

"engines": {
  "node": "^8 || ^10"
}

Would match either v8.x.x or v10.x.x but not v9.

You can read more about it here, or https://github.com/npm/node-semver#versions

ryenus
  • 15,711
  • 5
  • 56
  • 63
Gabe M
  • 2,391
  • 1
  • 15
  • 8
  • Do you know if the order matters? I mean, if I write it `^10 || ^8`, would it prioritize the version `^10` or not? – giovannipds May 30 '23 at 19:21
  • 1
    @giovannipds no it's an AND condition so either the version is support or not. You could add a hint during install if the user is running a deprecated Node version. Something like `"You are using Node v8. It will be deprecated in next release"`. Using the `"postinstall"` hook in package.json could work for custom logic. The node version can be checked from the ENV variable. – Gabe M Jun 02 '23 at 18:28
134

See the documentation which includes examples.

Provide a space separated list of engines with greater/less than symbols.

{ 
  "engines" : { 
    "node" : ">=8.0.0 <11.0.0" 
  }
}
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • 14
    As this includes the requested node versions it's exactly wrong, but it doesn't solve the actual problem as it would also include all of 9.x, which seems to be the actual problem here? – Flygenring Sep 27 '18 at 12:11
  • 3
    If you have two constraints like shown here... read "greater than 8 and less than 11" you can use a space. If you want to have multiple variants you must use the `||` – CWSites Mar 22 '21 at 23:39