yarn install failing mysteriously on Node.js v18.18

Published on in JavaScript

Last updated on

TL;DR: Upgrading Yarn from v1 to v4 fixed it for me.

Table of contents

Problem

Recently some CI pipelines[1] started to fail at yarn install (v1.22.19) with cryptic error messages like these (the error message kept changing):

  • An unexpected error occurred:
    "/path/to/project/node_modules/core-js-pure:
    Cannot read properties of undefined (reading 'getOption')".
    
  • An unexpected error occurred:
    "/path/to/project/node_modules/nodemon:
    Cannot read properties of undefined (reading 'getOption')".
    
  • An unexpected error occurred:
    "/path/to/project/node_modules/spawn-sync:
    Cannot read properties of undefined (reading 'config')".
    

Googling those error messages didn't yield anything useful.

Solution

The errors seemed to began after Node.js was upgraded from v18.16 to v18.18.

Googling "yarn install fails on node.js 18.18.2" (:D) led me to this GitHub comment on yarnpkg/yarn#3728:

6 years later, NPM removes the node-gyp binary from its bundle with v9.7.2 which is released with Node 18.18. Breaking our builds because now yarn tries to install node-gyp automatically and runs every now and then into this issue. Time to move away from yarn v1. :)

npm/cli#6554 (comment)

Leaving a comment primarily for others running into this problem now.

Upgrading Yarn to Yarn v4, which was released just a few days ago, did indeed fix the yarn install errors.

Side comments

  • Upgrading to Yarn v2 or v3 might have worked as well (I didn't test).
  • I wasn't even aware that there are multiple versions of Yarn. Maybe because it's not a "normal" dependency.
  • yarn install on Yarn v1 typically took about ~1.5 minutes in the CI pipelines, whereas on Yarn v4 it takes only about ~1 minute. A nice improvement!

The real lesson

The real lesson here is to pin exact dependency version numbers.

In my case Node.js was automatically upgraded from v18.16 to v18.18 because the version of Node.js was pinned to v18.

Had the Node.js version been pinned to v18.16.1 or v18.16 instead of just v18, these errors wouldn't have started suddenly.

Footnotes

  1. The CI pipelines were on Azure DevOps. That's likely irrelevant, but I still wanted to mention it, because:

    When investigating this problem, at first I didn't know what's causing it. One of the Google searches I made was "azure devops yarn install Cannot read properties of undefined (reading 'config" (:D).

    That search didn't yield any useful results. This page you are reading now would have been quite useful.