ds300 / patch-package

Fix broken node modules instantly 🏃🏽‍♀️💨
MIT License
10.31k stars 288 forks source link

[npm] Support lockfile version 3 #393

Closed brettwillis closed 1 year ago

brettwillis commented 2 years ago

Since NPM version 7, the package-lock.json version 2 file now uses a packages field, and the dependencies field is duplicated for backwards compatibility.

It is possible to configure NPM to use a package-lock.json version 3, which omits the dependencies field to greatly reduce the size of the lock file. See https://docs.npmjs.com/cli/v8/using-npm/config#lockfile-version.

To support lockfile-version=3, patch-package must be able to interpret the packages field of the lock file.

orta commented 1 year ago

Sure, open to PRs on this 👍🏻

quicksilverr commented 1 year ago

Can anyone explain, why this TypeError: Cannot read properties of undefined (reading 'dependencies') when trying to run npx patch-package error, is coming?

I'm unable to understand it.

ryaa commented 1 year ago

Can anyone explain, why this TypeError: Cannot read properties of undefined (reading 'dependencies') when trying to run npx patch-package error, is coming?

I'm unable to understand it.

The package-lock.json lockVersion 3 has different format than version 2. Specifically, "dependencies" node is not present as one of the top nodes in package-lock.json. The patch-package expect it to be there and fails when it is missing.

quicksilverr commented 1 year ago

And this also happens with yarn as well?

Can anyone explain, why this TypeError: Cannot read properties of undefined (reading 'dependencies') when trying to run npx patch-package error, is coming? I'm unable to understand it.

The package-lock.json lockVersion 3 has different format than version 2. Specifically, "dependencies" node is not present as one of the top nodes in package-lock.json. The patch-package expect it to be there and fails when it is missing.

quicksilverr commented 1 year ago

when I am doing yarn patch-package it's throwing the error. My bad, it seems the project has a package-lock.json as well. So, patch-package is defaulting to using npm.

--use-yarn flag, while using yarn doesn't throw the error.

anas10 commented 1 year ago

I've opened a PR to solve this issue here: https://github.com/ds300/patch-package/pull/434 I've tested it locally and it works fine. I've made the change in a way that should make it compatible with all versions of lockfile.

iTonyYo commented 1 year ago

modify this file directly 👉🏻 /node_modules/patch-package/dist/getPackageResolution.js with code below. then apply a patch.

const lockfile = require(path_1.join(appPath, packageManager === "npm-shrinkwrap"
    ? "npm-shrinkwrap.json"
    : "package-lock.json"));

if (lockfile.lockfileVersion > 2) {
    return Object.entries(lockfile.packages).find(el => el[0].includes(packageDetails.name))[1].resolved;
}

const lockFileStack = [lockfile];
for (const name of packageDetails.packageNames.slice(0, -1)) {
    const child = lockFileStack[0].dependencies;
    if (child && name in child) {
        lockFileStack.push(child[name]);
    }
}

lockFileStack.reverse();

const relevantStackEntry = lockFileStack.find((entry) => entry.dependencies && packageDetails.name in entry.dependencies);

const pkg = relevantStackEntry.dependencies[packageDetails.name];
return pkg.resolved || pkg.from || pkg.version;
Samuel-Therrien-Beslogic commented 11 months ago

This is happening to me with patch-package 7.0.0 and patch-package 8.0.0 on Node.js v18.14.0 with a "lockfileVersion": 3:

npx patch-package localForage 
patch-package 8.0.0
• Creating temporary folder
TypeError: Cannot read properties of undefined (reading 'dependencies')
    at Object.getPackageResolution (...\node_modules\patch-package\dist\getPackageResolution.js:98:40)
    at Object.makePatch (...\node_modules\patch-package\dist\makePatch.js:103:63)
    at ...\node_modules\patch-package\dist\index.js:72:25
    at Array.forEach (<anonymous>)
    at Object.<anonymous> (...\node_modules\patch-package\dist\index.js:71:22)
    at Module._compile (node:internal/modules/cjs/loader:1226:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1280:10)
    at Module.load (node:internal/modules/cjs/loader:1089:32)
    at Module._load (node:internal/modules/cjs/loader:930:12)
    at Module.require (node:internal/modules/cjs/loader:1113:19)
...\node_modules\patch-package\dist\makePatch.js:395
        throw e;
        ^

TypeError: Cannot read properties of undefined (reading 'dependencies')
    at Object.getPackageResolution (...\node_modules\patch-package\dist\getPackageResolution.js:98:40)
    at Object.makePatch (...\node_modules\patch-package\dist\makePatch.js:103:63)
    at ...\node_modules\patch-package\dist\index.js:72:25
    at Array.forEach (<anonymous>)
    at Object.<anonymous> (...\node_modules\patch-package\dist\index.js:71:22)
    at Module._compile (node:internal/modules/cjs/loader:1226:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1280:10)
    at Module.load (node:internal/modules/cjs/loader:1089:32)
    at Module._load (node:internal/modules/cjs/loader:930:12)
    at Module.require (node:internal/modules/cjs/loader:1113:19)

Node.js v18.14.0
alohaninja commented 3 months ago

Upgrading from patch-package 6.4.7 to 8.0.0 resolved this error for us!

TypeError: Cannot read properties of undefined (reading 'dependencies')