Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[npm] Support lockfile version 3 #393

Closed
brettwillis opened this issue May 15, 2022 · 9 comments · Fixed by #434
Closed

[npm] Support lockfile version 3 #393

brettwillis opened this issue May 15, 2022 · 9 comments · Fixed by #434

Comments

@brettwillis
Copy link

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 orta changed the title Support lockfile version 3 [npm] Support lockfile version 3 Sep 28, 2022
@orta
Copy link
Collaborator

orta commented Sep 28, 2022

Sure, open to PRs on this 👍🏻

@quicksilverr
Copy link

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
Copy link

ryaa commented Nov 9, 2022

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
Copy link

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
Copy link

quicksilverr commented Nov 9, 2022

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
Copy link

anas10 commented Nov 15, 2022

I've opened a PR to solve this issue here: #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
Copy link

iTonyYo commented Dec 8, 2022

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;

@orta orta closed this as completed in #434 Dec 29, 2022
@Samuel-Therrien-Beslogic

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
Copy link

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants