如果 npm 树中存在两个版本,如何使电子伪造和 webpack 加载特定版本的模块?

问题描述

这是 package.json

{
  "name": "IpfsPlaying","version": "2.0.2","description": "IpfsPlaying","main": ".webpack/main","scripts": {
    "start": "cross-env NODE_ENV=development electron-forge start","package": "electron-forge package","make": "electron-forge make","publish": "electron-forge publish","lint": "eslint --ext .ts ."
  },"config": {
    "forge": "./tools/forge/forge.config.js"
  },"devDependencies": {
    "@electron-forge/cli": "^6.0.0-beta.54","@electron-forge/maker-deb": "6.0.0-beta.53","@electron-forge/maker-rpm": "6.0.0-beta.53","@electron-forge/maker-squirrel": "^6.0.0-beta.54","@electron-forge/maker-zip": "6.0.0-beta.53","@electron-forge/plugin-webpack": "6.0.0-beta.53","@types/react": "^16.9.49","@types/react-dom": "^16.9.8","@types/webpack-env": "^1.15.2","@typescript-eslint/eslint-plugin": "^4.1.1","@typescript-eslint/parser": "^4.1.1","copy-webpack-plugin": "6","cross-env": "^7.0.2","electron": "^10.1.2","eslint": "^7.9.0","eslint-import-resolver-alias": "^1.1.2","eslint-plugin-import": "^2.20.0","eslint-plugin-react": "^7.20.6","fork-ts-checker-webpack-plugin": "^5.2.0","less": "^3.12.2","node-loader": "^1.0.1","react-hot-loader": "^4.12.21","ts-loader": "^8.0.3","typescript": "^4.0.2","webpack": "4"
 },"dependencies": {
    "@hot-loader/react-dom": "^16.13.0","ipfs": "^0.54.1","ipfs-http-client": "^49.0.1","ipfs-utils": "^6.0.0","react": "^16.13.1","react-dom": "^16.13.1","react-viewer": "^3.2.2","react-window": "^1.8.6"
  } 
}

在 main.ts 中使用 ipfs 时出现此错误:

A JavaScript error occurred in the main process
Uncaught Exception:
Error: Cannot find module 'jsonfile/utils'


A JavaScript error occurred in the main process
Uncaught Exception:
Error: Cannot find module 'jsonfile/utils'
    at webpackMissingModule (/home/marco/webMatters/electronMatters/IpfsPlaying/.webpack/main/index.js:78837:87)
    at Object../node_modules/fs-extra/lib/json/output-json.js (/home/marco/webMatters/electronMatters/IpfsPlaying/.webpack
/main/index.js:78837:176)
    at __webpack_require__ (/home/marco/webMatters/electronMatters/IpfsPlaying/.webpack/main/index.js:21:30)
    at Object../node_modules/fs-extra/lib/json/index.js (/home/marco/webMatters/electronMatters/IpfsPlaying/.webpack
/main/index.js:78765:25)
    at __webpack_require__ (/home/marco/webMatters/electronMatters/IpfsPlaying/.webpack/main/index.js:21:30)
    at Object../node_modules/fs-extra/lib/index.js (/home/marco/webMatters/electronMatters/IpfsPlaying/.webpack
/main/index.js:78731:6)
    at __webpack_require__ (/home/marco/webMatters/electronMatters/IpfsPlaying/.webpack/main/index.js:21:30)
    at Object../node_modules/ipfs-utils/src/files/glob-source.js (/home/marco/webMatters/electronMatters/IpfsPlaying/.webpack
/main/index.js:115083:12)
    at __webpack_require__ (/home/marco/webMatters/electronMatters/IpfsPlaying/.webpack/main/index.js:21:30)
    at Object../node_modules/ipfs-core/src/index.js (/home/marco/webMatters/electronMatters/IpfsPlaying/.webpack
/main/index.js:107575:20)

IPFS 项目的人,好心地对问题进行了粗略的分析,发现两个版本的 jsonfile 之间存在冲突:

我的 node_modules 文件夹中有两份 jsonfile:

$ find . -type d -name jsonfile
./node_modules/jsonfile                          <-- v4.0.0
./node_modules/fs-extra/node_modules/jsonfile    <-- v6.1.0

我删除了 ipfs 和最低限度的电子反应打字稿应用程序不需要的所有软件包。 并发现,根据 IPFS 的人已经发现的, 这些是依赖于 jsonfile 的模块:

(base) marco@pc01:~/webMatters/electronMatters/IpfsPlaying$ npm ls jsonfile
[email protected] /home/marco/webMatters/electronMatters/IpfsPlaying
├─┬ @electron-forge/[email protected]
│ ├─┬ @electron-forge/[email protected]
│ │ └─┬ [email protected]
│ │   └─┬ [email protected]
│ │     ├─┬ [email protected]
│ │     │ └─┬ [email protected]
│ │     │   └── [email protected]  deduped
│ │     └─┬ [email protected]
│ │       └── [email protected]  deduped
│ ├─┬ @electron/[email protected]
│ │ └─┬ [email protected]
│ │   └── [email protected] 
│ └─┬ [email protected]
│   └── [email protected] 
└─┬ @electron-forge/[email protected]
  └─┬ [email protected]
    └─┬ [email protected]
      └── [email protected]  deduped

如果我删除 jsonfile v4,我会收到错误消息:“Forge 内部发生了未处理的异常”:

(base) marco@pc01:~/webMatters/electronMatters/IpfsPlaying$ rm -rf ./node_modules/jsonfile/
(base) marco@pc01:~/webMatters/electronMatters/IpfsPlaying$ yarn start
yarn run v1.22.5
$ cross-env NODE_ENV=development electron-forge start

An unhandled exception has occurred inside Forge:
Cannot find module 'jsonfile'
Require stack:
- /home/marco/webMatters/electronMatters/IpfsPlaying/node_modules/@electron/get/node_modules/fs-extra/lib/json/jsonfile.js
- /home/marco/webMatters/electronMatters/IpfsPlaying/node_modules/@electron/get/node_modules/fs-extra/lib/json/index.js
- /home/marco/webMatters/electronMatters/IpfsPlaying/node_modules/@electron/get/node_modules/fs-extra/lib/index.js
- /home/marco/webMatters/electronMatters/IpfsPlaying/node_modules/@electron/get/dist/cjs/utils.js
- /home/marco/webMatters/electronMatters/IpfsPlaying/node_modules/@electron/get/dist/cjs/artifact-utils.js
- /home/marco/webMatters/electronMatters/IpfsPlaying/node_modules/@electron/get/dist/cjs/index.js
- /home/marco/webMatters/electronMatters/IpfsPlaying/node_modules/@electron-forge/core/dist/api/make.js
- /home/marco/webMatters/electronMatters/IpfsPlaying/node_modules/@electron-forge/core/dist/api/index.js
- /home/marco/webMatters/electronMatters/IpfsPlaying/node_modules/@electron-forge/cli/dist/util/check-system.js
- /home/marco/webMatters/electronMatters/IpfsPlaying/node_modules/@electron-forge/cli/dist/electron-forge.js
Error: Cannot find module 'jsonfile'
Require stack:
- /home/marco/webMatters/electronMatters/IpfsPlaying/node_modules/@electron/get/node_modules/fs-extra/lib/json/jsonfile.js
- /home/marco/webMatters/electronMatters/IpfsPlaying/node_modules/@electron/get/node_modules/fs-extra/lib/json/index.js
- /home/marco/webMatters/electronMatters/IpfsPlaying/node_modules/@electron/get/node_modules/fs-extra/lib/index.js
- /home/marco/webMatters/electronMatters/IpfsPlaying/node_modules/@electron/get/dist/cjs/utils.js
- /home/marco/webMatters/electronMatters/IpfsPlaying/node_modules/@electron/get/dist/cjs/artifact-utils.js
- /home/marco/webMatters/electronMatters/IpfsPlaying/node_modules/@electron/get/dist/cjs/index.js
- /home/marco/webMatters/electronMatters/IpfsPlaying/node_modules/@electron-forge/core/dist/api/make.js
- /home/marco/webMatters/electronMatters/IpfsPlaying/node_modules/@electron-forge/core/dist/api/index.js
- /home/marco/webMatters/electronMatters/IpfsPlaying/node_modules/@electron-forge/cli/dist/util/check-system.js
- /home/marco/webMatters/electronMatters/IpfsPlaying/node_modules/@electron-forge/cli/dist/electron-forge.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:1030:15)
    at Function.Module._load (internal/modules/cjs/loader.js:899:27)
    at Module.require (internal/modules/cjs/loader.js:1090:19)
    at require (internal/modules/cjs/helpers.js:75:18)
    at Object.<anonymous> (/home/marco/webMatters/electronMatters/IpfsPlaying/node_modules/@electron/get/node_modules
/fs-extra/lib/json/jsonfile.js:4:18)
    at Module._compile (internal/modules/cjs/loader.js:1201:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1221:10)
    at Module.load (internal/modules/cjs/loader.js:1050:32)
    at Function.Module._load (internal/modules/cjs/loader.js:938:14)
    at Module.require (internal/modules/cjs/loader.js:1090:19)

简而言之,问题在于electron-forge 使用的是jsonfile 的第4 版,而Ipfs 使用的是jsonfile 的第6 版。然后 webpack 加载了版本 4。 如果 npm 树中存在两个版本,如何使 Electron-forge 和 webpack 加载特定版本的模块?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)