ds300 / patch-package

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

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

Closed curious-george-nine closed 1 year ago

curious-george-nine commented 1 year ago

I tried to use this in my project. then it says an error. using npm

patch-package 6.5.0
• Creating temporary folder
TypeError: Cannot read properties of undefined (reading 'dependencies')
    at Object.getPackageResolution (path\to\node_modules\patch-package\dist\getPackageResolution.js:89:40)
    at Object.makePatch (path\to\node_modules\patch-package\dist\makePatch.js:52:63)
    at path\to\node_modules\patch-package\dist\index.js:53:25
    at Array.forEach (<anonymous>)
    at Object.<anonymous> (path\to\node_modules\patch-package\dist\index.js:52:22)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
path\to\node_modules\patch-package\dist\makePatch.js:218
        throw e;
        ^

TypeError: Cannot read properties of undefined (reading 'dependencies')
    at Object.getPackageResolution (path\to\node_modules\patch-package\dist\getPackageResolution.js:89:40)
    at Object.makePatch (path\to\node_modules\patch-package\dist\makePatch.js:52:63)
    at path\to\node_modules\patch-package\dist\index.js:53:25
    at Array.forEach (<anonymous>)
    at Object.<anonymous> (path\to\node_modules\patch-package\dist\index.js:52:22)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
zqianem commented 1 year ago

This is likely due to using a lockfileVersion of 3 (produces the same stack trace) and will be fixed by https://github.com/ds300/patch-package/pull/434.

alekseyBatuhtin commented 1 year ago

I solved the problem in this way

  1. converted package-lock to version=2
  2. npx patch-package your-package
  3. return back package-lock to version=3
el-j commented 1 year ago
  1. convert your lockfile to version 2
    npm shrinkwrap --lockfile-version 2
  2. edit node_modules/react-scripts/scripts/build.js and change line 12 | 13 + 58:
    
    // process.env.BABEL_ENV = "production"
    // process.env.NODE_ENV = "production"
    process.env.BABEL_ENV = process.env.NODE_ENV
    process.env.NODE_ENV = process.env.NODE_ENV

// const config = configFactory("production"); const config = configFactory(process.env.NODE_ENV);

3. use patch-package to create a patch:

npx patch-package react-script

4. **package.json** change `build` and add `build-dev`  + `postinstall` patch
```json
"scripts": {
...
"build": "NODE_ENV=production react-scripts build",
"build-dev": "NODE_ENV=development react-scripts build",
"postinstall": "npx patch-package"
...
},
  1. create lockfile version 3
    npm shrinkwrap --lockfile-version 3
  2. rename shrinkwrap.json to package-lock.json

thanks for the idea @alekseyBatuhtin @zqianem

tobua commented 1 year ago

Issue happens with npm 9.2.0, which is currently bundled with the latest stable node version. Installing node LTS with npm 8.19.2, removing the lockfile and generating a new one, can also be used as a workaround until #434 lands.

acoreyj commented 1 year ago

Anyone tried to use patch-package to patch patch-package so you can run patch-package?

tobua commented 1 year ago

Update has been released in 6.5.1 and is working fine, thanks!

DovletAmanov commented 1 year ago

In my case I realized that somehow there are both package-lock and yarn.lock in my project. I use yarn mainly so after deleting package-lock error gone.

Samuel-Therrien-Beslogic commented 10 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
vision-llz commented 9 months ago

就我而言,我意识到我的项目中同时存在package-lockyarn.lock。我主要使用纱线,所以删除后package-lock错误就消失了。

good idea