electron 为什么电子不能启动?

yftpprvb  于 2022-12-16  发布在  Electron
关注(0)|答案(2)|浏览(365)

我最近刚开始使用electron,使用的是here发布的教程。我根据所学内容为自己创建的项目模板的github是here。该repo的自述文件显示了我创建该模板时所做的所有事情。
到目前为止,一切都很顺利,至少在运行npm installnpm run build后的前几次运行npm run dev时是这样,在成功尝试了两三次之后,当我稍后返回项目尝试运行它时,并发插件将正确加载等待。但是当react-scripts成功地为react页面初始化本地服务器时,wait-on似乎永远不会触发。

PS D:\Home\Projects\finally> npm run dev
npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.

> finally@0.1.0 dev
> concurrently -k -s command-electron "cross-env BROWSER=none npm start" "npm:electron"

[electron] npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.
[0] npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.
[electron]
[electron] > finally@0.1.0 electron
[electron] > wait-on tcp:3000 && electron .
[electron]
[0]
[0] > finally@0.1.0 start
[0] > react-scripts start
[0]
[0] (node:4504) [DEP_WEBPACK_DEV_SERVER_ON_AFTER_SETUP_MIDDLEWARE] DeprecationWarning: 'onAfterSetupMiddleware' option is deprecated. Please use the 'setupMiddlewares' option.
[0] (Use `node --trace-deprecation ...` to show where the warning was created)
[0] (node:4504) [DEP_WEBPACK_DEV_SERVER_ON_BEFORE_SETUP_MIDDLEWARE] DeprecationWarning: 'onBeforeSetupMiddleware' option is deprecated. Please use the 'setupMiddlewares' option.
[0] Starting the development server...
[0]
[0] Compiled successfully!
[0]
[0] You can now view finally in the browser.
[0]
[0]   Local:            http://localhost:3000
[0]   On Your Network:  http://172.28.224.1:3000
[0]
[0] Note that the development build is not optimized.
[0] To create a production build, use npm run build.
[0]
[0] webpack compiled successfully
[0] No issues found.

此时它再也不会继续,只是挂起,电子窗口也不会填充。我试过几次重新创建repo,删除build和node_modules并重新安装,但似乎没有什么共同因素。
另外需要注意的是,这是没有添加任何超出模板提供的代码。我克隆了模板,尝试运行它,它工作了几次,然后就没有了。
有什么见解吗?

sz81bmfz

sz81bmfz1#

这可能是由node.js更新引起的。

这就解决了

1.转到node.js安装文件夹,例如C:\Program Files\nodejs
1.以管理员身份使用记事本打开npm.cmd
1.通过将prefix -g替换为prefix --location=global来编辑文件,然后保存
1.对npx.cmd也进行相同的更改
测试是否已修复
如果未修复,请尝试使用npm install npm@latest -g更新npm

bakd9h0s

bakd9h0s2#

我有山姆的问题,答案在这里:https://dev.to/brittanynavin/comment/1ledd
因此,在package.json中,只需将http://localhost:3000更改为启动脚本的IP地址。

"scripts": {
    "start": "concurrently \"cross-env BROWSER=none npm run react-start\" \"wait-on http://127.0.0.1:3000 && electronmon .\"",
    "build": "react-scripts build",
    "test": "mocha",
    "eject": "react-scripts eject",
    "dist:win": "npm run build && electron-builder -w -c.extraMetadata.main=build/electron.js",
    "dist:mac": "npm run build && electron-builder -m -c.extraMetadata.main=build/electron.js",
    "dist:linux": "npm run build && electron-builder -l -c.extraMetadata.main=build/electron.js",
    "react-start": "react-scripts start"
  },

相关问题