FG
๐Ÿ› ๏ธ Developer ToolsMicrosoft

[BUG] Platform-specific optional dependencies not being included in `package-lock.json` when reinstalling with `node_modules` present

Freshabout 20 hours ago
Mar 14, 20260 views
Confidence Score95%
95%

Problem

Is there an existing issue for this? - [X] I have searched the existing issues This issue exists in the latest npm version - [X] I am using the latest npm Current Behavior [code block] I'm working on a team that utilizes a mix of x64-based and m1-based macs, and has CI build processes that uses musl. We're seeing that `npm` is skipping platform-specific optional dependencies for packages such as `@swc/core` as a result of the `package-lock.json` file being generated without all of them included. In our case, this then causes linting to throw an exception, because one of our eslint plugins depends on @swc, which depends on having the platform specific @swc package also installed. There seems to be at least two stages of cause to this. Firstly, when installing `@swc/core` from a clean slate working directory `npm` generates a `package-lock.json` with all of the optional dependencies for `@swc/core` listed: [code block] And it only installs the platform specific package: [code block] If I then remove my `package-lock.json`, leave my `node_modules` directory as-is, and then reinstall, I get: [code block] That is, it then generates a package-lock.json with only the platform-specific dependency that was installed on this machine, and not with the other optional dependencies that should also be listed. If you delete both `node_modules` AND `package-lock.json`, and then re-run `npm install`, it generates the correct lockfile with all of those optional dependencies list

Unverified for your environment

Select your OS to check compatibility.

2 Fixes

Canonical Fix
Unverified Fix
New Fix โ€“ Awaiting Verification

Ensure All Platform-Specific Optional Dependencies Are Included in package-lock.json

Medium Risk

The issue arises because npm generates the package-lock.json file based on the currently installed node_modules directory. When node_modules exists, npm does not reevaluate optional dependencies for different platforms, leading to missing entries in the lockfile. This behavior is particularly problematic in environments with mixed architectures (e.g., x64 and ARM) where optional dependencies vary by platform.

Awaiting Verification

Be the first to verify this fix

  1. 1

    Remove node_modules and package-lock.json

    To ensure a clean slate for dependency installation, delete both the node_modules directory and the package-lock.json file. This forces npm to reevaluate all dependencies, including optional ones.

    bash
    rm -rf node_modules package-lock.json
  2. 2

    Reinstall Dependencies

    Run npm install to regenerate the package-lock.json file with all optional dependencies included. This ensures that the lockfile reflects all dependencies required for the project across different platforms.

    bash
    npm install
  3. 3

    Verify package-lock.json

    Open the newly generated package-lock.json file and check for the presence of all platform-specific optional dependencies for packages like @swc/core. Confirm that the correct versions for each platform are listed.

    bash
    cat package-lock.json | grep '@swc/core'
  4. 4

    Test Linting

    Run the linting process to ensure that all necessary dependencies are correctly installed and that no exceptions are thrown. This will confirm that the optional dependencies are functioning as expected.

    bash
    npm run lint
  5. 5

    Document the Fix

    Update the project documentation to include a note about the importance of removing node_modules and package-lock.json before reinstalling dependencies in mixed architecture environments to avoid similar issues in the future.

Validation

Confirm that the package-lock.json file includes all necessary platform-specific optional dependencies and that the linting process runs without errors. If the linting passes and the lockfile is correct, the fix is successful.

Sign in to verify this fix

1 low-confidence fix
Unverified Fix
New Fix โ€“ Awaiting Verification

Fix platform-specific optional dependencies not included in package-lock.json

Medium Risk

The issue arises because npm does not correctly handle optional dependencies when the package-lock.json file is present and node_modules already exists. When reinstalling, npm skips the inclusion of optional dependencies that are not installed on the current platform, leading to incomplete package-lock.json files.

Awaiting Verification

Be the first to verify this fix

  1. 1

    Remove existing package-lock.json and node_modules

    To ensure that npm can regenerate the package-lock.json with all optional dependencies, start by removing both the package-lock.json file and the node_modules directory.

    bash
    rm -rf package-lock.json node_modules
  2. 2

    Reinstall dependencies

    Run npm install to regenerate the package-lock.json file and install all dependencies, including optional ones for the current platform.

    bash
    npm install
  3. 3

    Verify package-lock.json

    Check the generated package-lock.json to ensure that all platform-specific optional dependencies are included.

    bash
    cat package-lock.json | grep '@swc/core'
  4. 4

    Test linting

    Run your linting process to confirm that the required optional dependencies are correctly installed and that there are no exceptions thrown.

    bash
    npm run lint
  5. 5

    Document the fix

    Update your project documentation to include a note about the necessity of removing package-lock.json and node_modules before reinstalling to avoid this issue in the future.

Validation

Confirm that the package-lock.json includes all necessary optional dependencies for your platform and that the linting process completes without errors.

Sign in to verify this fix

Environment

Submitted by

AC

Alex Chen

2450 rep

Tags

npmpackage-managernodejsrelease-8.xbugneeds-triage