[Solved] Yarn update not working because of a bug in node-gyp – Node-gyp

by
Ali Hasan
node-gyp node.js npm

Quick Fix: Verify for dependency updates. Determine the module causing the error, like ‘node-sass’ in this case, and then update the dependency.

The Problem:

Node.js users are encountering a puzzling issue where the ‘yarn update’ command fails with a SyntaxError in the ‘common.gypi’ file. This occurs specifically after installing Node.js versions 18.7.0 or 20.5.0 via nvm on a Windows Subsystem for Linux 2 (WSL2) environment. The ‘common.gypi’ file appears truncated, causing the error. Can you provide a solution to resolve this issue?

The Solutions:

\n

Solution 1: Update the Incompatible Dependency

\n

The yarn update issue is most likely caused by an incompatible dependency in your project that is not compatible with NodeJS 18 or 20. To resolve this issue, follow these steps:

  1. Before the Python invocation, identify the module where the common.gypi file is originating from. This can be done by checking the error message, which should provide some clues about the problematic module.

  2. Once you have identified the problematic dependency (e.g., node-sass), update it to a compatible version using the following command:

npm update <dependency name> –save

  1. This command should update the dependency to the latest compatible version, resolving the yarn update issue.

  2. If the dependency cannot be updated due to version compatibility issues with other dependencies, you may need to find an alternative module or investigate further to resolve the incompatibility.

Solution 2: Set the Correct Python Version

The issue seems to be related to the version of Python used during the installation of Node.js modules. Node-gyp, a tool used for compiling native C++ modules, specifies on its README that specific versions of Python are supported (currently Python 3.7, 3.8, 3.9, or 3.10).

To resolve the issue, you need to ensure that the correct Python version is being used when installing npm dependencies. There are two ways to do this:

  1. Using npm Config:

    You can set the Python version to be used by running the following command in your terminal:

    npm config set python /usr/bin/python3

    If you receive an error message stating “not a valid npm option,” you can edit the npm config directly using the following command:

    npm config edit

    In the config file, add the following two lines:

    “`
    python=/usr/bin/python3
    python3=/usr/bin/python3
    “`
  2. Setting Environment Variables:

    Alternatively, you can set the PYTHON and PYTHON3 environment variables to point to the desired Python version. Run the following commands in your terminal:

    export PYTHON=/usr/bin/python3

    export PYTHON3=/usr/bin/python3

After setting the Python version, try running “yarn” or “yarn update” again. This should resolve the issue with the truncated “commone.gypi” file and allow yarn to install the dependencies correctly.

Q&A

Why does yarn update not work after installing NodeJS 18.7.0 or 20.5.0 on WSL2 with nvm?

Dependencies not compatible with NodeJS 18 or 20 may be causing the issue.

What is the recommended Python version to use with node-gyp?

Python version 3.7, 3.8, 3.9, or 3.10.

How to set the desired Python version?

Use npm config set python /usr/bin/python3 or edit the npm config with npm config edit.

Video Explanation:

The following video, titled "Npm ERR code ERESOLVE react | Npm install error in visual studio ...", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

Go to channel · npm start not working / How to fix npm error React /npm start error node js /npm start in vs code. Coding.cherry•219K views · 1: ...