无法读取未定义的属性“createSnapshot”

Posted

技术标签:

【中文标题】无法读取未定义的属性“createSnapshot”【英文标题】:Cannot read property 'createSnapshot' of undefined 【发布时间】:2021-04-26 13:59:48 【问题描述】:

由于我使用了Electron forge webpack plugin,所以当我执行npm start时,会在Compiling Preload Scripts这一步之后报错。它说错误是在 Forge 内部。 我查了一下,是file watcher api中的错误,mainCompilation.fileSystemInfo是未定义的,所以它无法读取prop createSnapShot函数。

我应该怎么做才能解决这个错误并启动我的应用程序?

日志:

> datapack-planet@0.0.0 start D:\program\datapack-planet
> electron-forge start

√ Checking your system
√ Locating Application
You have set packagerConfig.ignore, the Electron Forge webpack plugin normally sets this automatically.

Your packaged app may be larger than expected if you dont ignore everything other than the '.webpack' folder
√ Preparing native dependencies
√ Compiling Main Process Code
√ Launch Dev Servers
√ Compiling Preload Scripts

An unhandled rejection has occurred inside Forge:
TypeError: Cannot read property 'createSnapshot' of undefined
    at D:\program\datapack-planet\node_modules\html-webpack-plugin\lib\webpack5\file-watcher-api.js:13:36
    at new Promise (<anonymous>)
    at Object.createSnapshot (D:\program\datapack-planet\node_modules\html-webpack-plugin\lib\webpack5\file-watcher-api.js:12:10)
    at D:\program\datapack-planet\node_modules\html-webpack-plugin\lib\cached-child-compiler.js:219:35
    at processTicksAndRejections (internal/process/task_queues.js:93:5)

Electron Forge was terminated. Location:

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! datapack-planet@0.0.0 start: `electron-forge start`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the datapack-planet@0.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\dell\AppData\Roaming\npm-cache\_logs\2021-01-22T04_33_47_591Z-debug.log

我的package.json(不是全部,仅是重要数据):


  "main": "./.webpack/main",
  "devDependencies": 
    "@electron-forge/cli": "^6.0.0-beta.54",
    "@electron-forge/maker-deb": "^6.0.0-beta.54",
    "@electron-forge/maker-rpm": "^6.0.0-beta.54",
    "@electron-forge/maker-squirrel": "^6.0.0-beta.54",
    "@electron-forge/maker-zip": "^6.0.0-beta.54",
    "@electron-forge/plugin-webpack": "^6.0.0-beta.54",
    "@marshallofsound/webpack-asset-relocator-loader": "^0.5.0",
    "@types/jquery": "^3.5.5",
    "@types/node": "^14.14.22",
    "electron": "^11.1.1",
    "fork-ts-checker-webpack-plugin": "^6.1.0",
    "html-loader": "^1.3.2",
    "jquery": "^3.5.1",
    "less": "^4.1.0",
    "less-loader": "^7.2.1",
    "node-loader": "^1.0.2",
    "ts-loader": "^8.0.14",
    "typescript": "^4.1.3",
    "webpack": "^5.16.0",
    "webpack-cli": "^4.4.0"
  ,
  "dependencies": 
    "electron-squirrel-startup": "^1.0.0",
    "jquery": "^3.5.1"
  ,
  "scripts": 
    "start": "electron-forge start",
    "package": "electron-forge package",
    "make": "electron-forge make"
  ,
  "config": 
    "forge": 
      "packagerConfig": 
        "ignore": [
          "\\.(idea|git)",
          "config\\.json",
          "[A-Z]",
          "md$"
        ]
      ,
      "makers": [
        // There is some auto-generated settings, I didn't changed it.
        // So I skip those configs.
      ],
      "plugins": [
        [
          "@electron-forge/plugin-webpack",
          
            "mainConfig": "./webpack.main.config.js",
            "renderer": 
              "config": "./webpack.renderer.config.js",
              "entryPoints": [
                
                  "html": "./src/index.html",
                  "js": "./src/renderer.ts",
                  "name": "main_window"
                
              ]
            
          
        ]
      ]
    
  

解决:我重新启动项目。也许我错过了一些依赖项。我是从模板中复制的,所以应该会按预期发生错误。

【问题讨论】:

【参考方案1】:

这是由于您的 webpack 版本不匹配,只需检查 npm list webpack,您将能够看到类似未满足的依赖项。只需创建 peerDependencies 并将 webpack 添加到其中,或者可以删除 webpack inteself。检查您的 ts-loader 版本,它应该小于 9。看看魔法:)

【讨论】:

【参考方案2】:

我的解决方法是删除node_modulespackage-lock.json,然后运行

npm install --legacy-peer-deps

非常现代的 npm 版本存在问题,这为您提供了似乎有效的旧版安装规则

【讨论】:

【参考方案3】:

我也遇到了同样的问题,似乎是compatibility issue with npm7 and plugin-webpack。

有一些解决方案,但对我来说最简单的方法是简单地使用 yarn 而不是 npm

删除node_modules/ 删除package-lock.json 运行yarn install 运行yarn start

这对我有用,如果有帮助,请告诉我!

【讨论】:

该链接包含有关仍使用 npm 的修复的信息,供其他感兴趣的人使用。这帮助了我。【参考方案4】:

我遇到了同样的问题,在检查并更正安装的版本后得到了解决。

运行npm ls webpack 以显示缺少对等依赖项。这将帮助您确定应用所需的正确版本。

更改为所需版本,保存 package.json 文件。

删除package-lock.jsonnode_modules 文件夹。

运行npm install

再次运行脚本。

【讨论】:

我正在通过npx create-electron-app electron --template=webpack 进行全新安装,这些说明并不能解决问题【参考方案5】:

在项目内部运行npm installyarn,安装项目的依赖项

【讨论】:

以上是关于无法读取未定义的属性“createSnapshot”的主要内容,如果未能解决你的问题,请参考以下文章

带有 Ionic 4 的 SQLite?无法读取未定义类型错误的属性“then”:无法读取未定义的属性“then”

`无法读取未定义的属性(读取'组件')`

NextJS:未捕获的类型错误:无法读取未定义的属性(读取“属性”)

使用地图时反应'无法读取未定义的属性'

如何使用自定义错误消息捕获“TypeError:无法读取未定义的属性(读取'0')”?

查询返回错误“无法读取未定义的属性(读取'节点')”