electron打包遇到的问题

这是我配置的scripts:

"scripts": {
    "start": "node scripts/start.js",
    "build": "rm -rf ./build && node scripts/build.js",
    "test": "node scripts/test.js",
    "electron": "electron .",
    "rebuild": "electron-rebuild -f -w sqlite3",
    "pack": "electron-builder --dir",
    "dist": "electron-builder",
    "postinstall": "electron-builder install-app-deps",
    "electron-pack": "rm -rf ./dist &&build -w"
  },

我用的react和electron开发应用,打包命令是这个:

 npm run-script build && yarn electron-pack

前面是打包react的,后面是打包electron的,是我在package中配置的,但是一运行,(这是在git bush中运行的,还有乱码,所以下面换到了cmder)报错如下图:
electron打包遇到的问题_第1张图片
electron打包遇到的问题_第2张图片
electron打包遇到的问题_第3张图片
electron打包遇到的问题_第4张图片
electron打包遇到的问题_第5张图片

真的,要疯了,一运行 yarn electron-pack就报错,换个命令yarn dist 一样报错,直接上报错内容:

E:\aPritice\JieDe\sqlite\sqlite>yarn dist
yarn dist v0.21.3
(node:55876) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
$ electron-builder
  • electron-builder version=20.38.5
  • loaded configuration file=package.json ("build" field)
  • description is missed in the package.json appPackageFile=E:\aPritice\JieDe\sqlite\sqlite\package.json
  • author is missed in the package.json appPackageFile=E:\aPritice\JieDe\sqlite\sqlite\package.json
  • electron-rebuild not required if you use electron-builder, please consider to remove excess dependency from devDependencies

To ensure your native dependencies are always matched electron version, simply add script `"postinstall": "electron-builder install-app-deps" to your `package.json`
  • writing effective config file=dist\builder-effective-config.yaml
  • rebuilding native production dependencies platform=win32 arch=x64
  • rebuilding native dependency name=buffertools
Error: C:\Program Files\nodejs\node.exe exited with code 1
Output:
yarn run v0.21.3
$ node-gyp rebuild

To ensure your native dependencies are always matched electron version, simply add script `"postinstall": "electron-builder install-app-deps" to your `package.json`
  • writing effective config file=dist\builder-effective-config.yaml
  • rebuilding native production dependencies platform=win32 arch=x64
  • rebuilding native dependency name=buffertools
Error: C:\Program Files\nodejs\node.exe exited with code 1
Output:
yarn run v0.21.3
$ node-gyp rebuild

E:\aPritice\JieDe\sqlite\sqlite\node_modules\buffertools>if not defined npm_config_node_gyp (node "E:\Program Files (x86)\Yarn\bin\node-gyp-bin\\..\..\node_modules\node-gyp\bin\node-gyp.js" rebuild )  else (node  rebuild )
在此解决方案中一次生成一个项目。若要启用并行生成,请添加“/m”开关。
MSBUILD : error MSB3428: 未能加载 Visual C++ 组件“VCBuild.exe”。要解决此问题,1) 安装 .NET Framework 2.0 SDK;2) 安装 Microsoft Visual Studio 2005
;或 3) 如果将该组件安装到了其他位置,请将其位置添加到系统路径中。 [E:\aPritice\JieDe\sqlite\sqlite\node_modules\buffertools\build\binding.sln]
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Error output:
(node:53392) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
gyp info it worked if it ends with ok
gyp info using [email protected]
gyp info using [email protected] | win32 | x64
gyp info spawn E:\Python27\python.EXE
gyp info spawn args [ 'E:\\Program Files (x86)\\Yarn\\node_modules\\node-gyp\\gyp\\gyp_main.py',
gyp info spawn args   'binding.gyp',
gyp info spawn args   '-f',
gyp info spawn args   'msvs',
gyp info spawn args   '-G',
gyp info spawn args   'msvs_version=auto',
gyp info spawn args   '-I',
gyp info spawn args   'E:\\aPritice\\JieDe\\sqlite\\sqlite\\node_modules\\buffertools\\build\\config.gypi',
gyp info spawn args   '-I',
gyp info spawn args   'E:\\Program Files (x86)\\Yarn\\node_modules\\node-gyp\\addon.gypi',
gyp info spawn args   '-I',
gyp info spawn args   'C:\\Users\\zoe\\.electron-gyp\\4.1.4\\include\\node\\common.gypi',
gyp info spawn args   '-Dlibrary=shared_library',
gyp info spawn args   '-Dvisibility=default',
gyp info spawn args   '-Dnode_root_dir=C:\\Users\\zoe\\.electron-gyp\\4.1.4',
gyp info spawn args   '-Dnode_gyp_dir=E:\\Program Files (x86)\\Yarn\\node_modules\\node-gyp',
gyp info spawn args   '-Dnode_lib_file=node.lib',
gyp info spawn args   '-Dmodule_root_dir=E:\\aPritice\\JieDe\\sqlite\\sqlite\\node_modules\\buffertools',
gyp info spawn args   '--depth=.',
gyp info spawn args   '--no-parallel',
gyp info spawn args   '--generator-output',
gyp info spawn args   'E:\\aPritice\\JieDe\\sqlite\\sqlite\\node_modules\\buffertools\\build',
gyp info spawn args   '-Goutput_dir=.' ]
Warning: unrecognized setting VCCLCompilerTool/MultiProcessorCompilation
Warning: unrecognized setting VCCLCompilerTool/MultiProcessorCompilation
gyp info spawn C:\Program Files (x86)\MSBuild\14.0\bin\msbuild.exe
gyp info spawn args [ 'build/binding.sln',
gyp info spawn args   '/clp:Verbosity=minimal',
gyp info spawn args   '/nologo',
gyp info spawn args   '/p:Configuration=Release;Platform=x64' ]
gyp ERR! build error
gyp ERR! stack Error: `C:\Program Files (x86)\MSBuild\14.0\bin\msbuild.exe` failed with exit code: 1
gyp ERR! stack     at ChildProcess.onExit (E:\Program Files (x86)\Yarn\node_modules\node-gyp\lib\build.js:276:23)
gyp ERR! stack     at ChildProcess.emit (events.js:182:13)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:240:12)
gyp ERR! System Windows_NT 10.0.17134
gyp ERR! command "C:\\Program Files\\nodejs\\node.exe" "E:\\Program Files (x86)\\Yarn\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild"
gyp ERR! cwd E:\aPritice\JieDe\sqlite\sqlite\node_modules\buffertools
gyp ERR! node -v v10.15.0
gyp ERR! node-gyp -v v3.5.0
gyp ERR! not ok
error Command failed with exit code 1.

    at ChildProcess.childProcess.once.code (C:\Users\zoe\AppData\Roaming\npm\node_modules\electron-builder\node_modules\builder-util\src\util.ts:244:14)
    at Object.onceWrapper (events.js:273:13)
    at ChildProcess.emit (events.js:182:13)
    at maybeClose (internal/child_process.js:962:16)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:251:5)
From previous event:
    at runCallback (timers.js:705:18)
    at tryOnImmediate (timers.js:676:5)
    at processImmediate (timers.js:658:5)
From previous event:
    at C:\Users\zoe\AppData\Roaming\npm\node_modules\electron-builder\node_modules\app-builder-lib\src\util\yarn.ts:155:24
From previous event:
    at _rebuild (C:\Users\zoe\AppData\Roaming\npm\node_modules\electron-builder\node_modules\app-builder-lib\src\util\yarn.ts:179:5)
    at rebuild (C:\Users\zoe\AppData\Roaming\npm\node_modules\electron-builder\node_modules\app-builder-lib\out\util\yarn.js:163:19)
    at C:\Users\zoe\AppData\Roaming\npm\node_modules\electron-builder\node_modules\app-builder-lib\src\util\yarn.ts:20:11
    at Generator.next ()
    at runCallback (timers.js:705:18)
    at tryOnImmediate (timers.js:676:5)
    at processImmediate (timers.js:658:5)
From previous event:
    at _installOrRebuild (C:\Users\zoe\AppData\Roaming\npm\node_modules\electron-builder\node_modules\app-builder-lib\out\util\yarn.js:71:28)
    at installOrRebuild (C:\Users\zoe\AppData\Roaming\npm\node_modules\electron-builder\node_modules\app-builder-lib\out\util\yarn.js:55:28)
    at C:\Users\zoe\AppData\Roaming\npm\node_modules\electron-builder\node_modules\app-builder-lib\src\packager.ts:511:13
    at Generator.next ()
From previous event:
    at Packager.installAppDependencies (C:\Users\zoe\AppData\Roaming\npm\node_modules\electron-builder\node_modules\app-builder-lib\src\packager.ts:472:70)
    at C:\Users\zoe\AppData\Roaming\npm\node_modules\electron-builder\node_modules\app-builder-lib\src\packager.ts:420:20
    at Generator.next ()
From previous event:
    at Packager.doBuild (C:\Users\zoe\AppData\Roaming\npm\node_modules\electron-builder\node_modules\app-builder-lib\src\packager.ts:396:24)
    at C:\Users\zoe\AppData\Roaming\npm\node_modules\electron-builder\node_modules\app-builder-lib\src\packager.ts:366:57
    at Generator.next ()
    at C:\Users\zoe\AppData\Roaming\npm\node_modules\electron-builder\node_modules\graceful-fs\graceful-fs.js:111:16
    at C:\Users\zoe\AppData\Roaming\npm\node_modules\electron-builder\node_modules\graceful-fs\graceful-fs.js:45:10
    at FSReqWrap.oncomplete (fs.js:141:20)
From previous event:
    at Packager._build (C:\Users\zoe\AppData\Roaming\npm\node_modules\electron-builder\node_modules\app-builder-lib\src\packager.ts:335:133)
    at C:\Users\zoe\AppData\Roaming\npm\node_modules\electron-builder\node_modules\app-builder-lib\src\packager.ts:331:23
    at Generator.next ()
    at runCallback (timers.js:705:18)
    at tryOnImmediate (timers.js:676:5)
    at processImmediate (timers.js:658:5)
From previous event:
    at Packager.build (C:\Users\zoe\AppData\Roaming\npm\node_modules\electron-builder\node_modules\app-builder-lib\src\packager.ts:288:14)
    at build (C:\Users\zoe\AppData\Roaming\npm\node_modules\electron-builder\node_modules\app-builder-lib\src\index.ts:59:28)
    at build (C:\Users\zoe\AppData\Roaming\npm\node_modules\electron-builder\src\builder.ts:227:10)
    at then (C:\Users\zoe\AppData\Roaming\npm\node_modules\electron-builder\src\cli\cli.ts:46:19)
error Command failed with exit code 1.

问了好多人,自己百度谷歌,折腾了两天多,要崩溃了感觉,谢谢郭志浩童鞋,谢谢帮助过我的苏南大叔,谢谢在electron群里的好心人!
因为我嵌入了pcsclite ,一直没有打包过,本地运行可以,但是一打包就报错,我的package.json有问题,改了很多次,少配置项:
"files": [ "build/**/*", "node_modules/**/*", "public/main.js", "src/api/**/*" ],
猜测1:
我以为是我没写作者和描述,加上了:
electron打包遇到的问题_第6张图片
写完,果然没有了最上面的author和description报错,但是不解决问题:
electron打包遇到的问题_第7张图片
猜测2:
报的错

以为是未能加载 Visual C++ 组件“VCBuild.exe”的问题,所以我就用管理员权限运行了一下命令:

npm install -g --production windows-build-tools

一直卡住不动,可能是我网的问题,后来终于执行完了,我执行打包命令,依然报错,不能解决我的问题(之前下载pcsclite使用过这个命令不顶事,这次依旧,可能我电脑配置有问题)
后来换了一种方法:
把我之前下载的visual status C++,卸载了,然后我就找不到了,谢谢苏南大叔的博客:
博客地址,我下载了好几个,最后找到直接下载visual statiu 15 C++工具地址https://download.visualstudio.microsoft.com/download/pr/11503713/e64d79b40219aea618ce2fe10ebd5f0d/vs_BuildTools.exe,重新下载

下载完直接打开
然后选择
图片:
electron打包遇到的问题_第8张图片

xia2

一共7个G,很漫长,好容易完事了,代码又可以正常运行了,但是,还是不能打包;

解决步骤1:
打包就报错,和上方一样,就有童鞋告诉我,让我运行一个命令:npm config set msvs_version 2017
不过还是不行,庆幸报的错不一样了,说明是有作用的

E:\aPritice\JieDe\sqlite\sqlite>yarn dist
yarn dist v0.21.3
(node:54240) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
$ electron-builder
  • electron-builder version=20.38.5
  • loaded configuration file=package.json ("build" field)
  • writing effective config file=dist\builder-effective-config.yaml
  • rebuilding native production dependencies platform=win32 arch=x64
  • rebuilding native dependency name=buffertools
Error: C:\Program Files\nodejs\node.exe exited with code 1
Output:
yarn run v0.21.3
$ node-gyp rebuild

E:\aPritice\JieDe\sqlite\sqlite\node_modules\buffertools>if not defined npm_config_node_gyp (node "E:\Program Files (x86)\Yarn\bin\node-gyp-bin\\..\..\node_modules\node-gyp\bin\node-gyp.js" rebuild )  else (node  rebuild )
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Error output:
(node:73712) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
gyp info it worked if it ends with ok
gyp info using [email protected]
gyp info using [email protected] | win32 | x64
gyp info spawn E:\Python27\python.EXE
gyp info spawn args [ 'E:\\Program Files (x86)\\Yarn\\node_modules\\node-gyp\\gyp\\gyp_main.py',
gyp info spawn args   'binding.gyp',
gyp info spawn args   '-f',
gyp info spawn args   'msvs',
gyp info spawn args   '-G',
gyp info spawn args   'msvs_version=2017',
gyp info spawn args   '-I',
gyp info spawn args   'E:\\aPritice\\JieDe\\sqlite\\sqlite\\node_modules\\buffertools\\build\\config.gypi',
gyp info spawn args   '-I',
gyp info spawn args   'E:\\Program Files (x86)\\Yarn\\node_modules\\node-gyp\\addon.gypi',
gyp info spawn args   '-I',
gyp info spawn args   'C:\\Users\\zoe\\.electron-gyp\\4.1.4\\include\\node\\common.gypi',
gyp info spawn args   '-Dlibrary=shared_library',
gyp info spawn args   '-Dvisibility=default',
gyp info spawn args   '-Dnode_root_dir=C:\\Users\\zoe\\.electron-gyp\\4.1.4',
gyp info spawn args   '-Dnode_gyp_dir=E:\\Program Files (x86)\\Yarn\\node_modules\\node-gyp',
gyp info spawn args   '-Dnode_lib_file=node.lib',
gyp info spawn args   '-Dmodule_root_dir=E:\\aPritice\\JieDe\\sqlite\\sqlite\\node_modules\\buffertools',
gyp info spawn args   '--depth=.',
gyp info spawn args   '--no-parallel',
gyp info spawn args   '--generator-output',
gyp info spawn args   'E:\\aPritice\\JieDe\\sqlite\\sqlite\\node_modules\\buffertools\\build',
gyp info spawn args   '-Goutput_dir=.' ]
Traceback (most recent call last):
  File "E:\Program Files (x86)\Yarn\node_modules\node-gyp\gyp\gyp_main.py", line 16, in 
    sys.exit(gyp.script_main())
  File "E:\Program Files (x86)\Yarn\node_modules\node-gyp\gyp\pylib\gyp\__init__.py", line 545, in script_main
    return main(sys.argv[1:])
  File "E:\Program Files (x86)\Yarn\node_modules\node-gyp\gyp\pylib\gyp\__init__.py", line 538, in main
    return gyp_main(args)
  File "E:\Program Files (x86)\Yarn\node_modules\node-gyp\gyp\pylib\gyp\__init__.py", line 514, in gyp_main
    options.duplicate_basename_check)
  File "E:\Program Files (x86)\Yarn\node_modules\node-gyp\gyp\pylib\gyp\__init__.py", line 98, in Load
    generator.CalculateVariables(default_variables, params)
  File "E:\Program Files (x86)\Yarn\node_modules\node-gyp\gyp\pylib\gyp\generator\msvs.py", line 1900, in CalculateVariables
    generator_flags.get('msvs_version', 'auto'))
  File "E:\Program Files (x86)\Yarn\node_modules\node-gyp\gyp\pylib\gyp\MSVSVersion.py", line 434, in SelectVisualStudioVersion
    versions = _DetectVisualStudioVersions(version_map[version], 'e' in version)
KeyError: '2017'
gyp ERR! configure error
gyp ERR! stack Error: `gyp` failed with exit code: 1
gyp ERR! stack     at ChildProcess.onCpExit (E:\Program Files (x86)\Yarn\node_modules\node-gyp\lib\configure.js:308:16)
gyp ERR! stack     at ChildProcess.emit (events.js:182:13)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:240:12)
gyp ERR! System Windows_NT 10.0.17134
gyp ERR! command "C:\\Program Files\\nodejs\\node.exe" "E:\\Program Files (x86)\\Yarn\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild"
gyp ERR! cwd E:\aPritice\JieDe\sqlite\sqlite\node_modules\buffertools
gyp ERR! node -v v10.15.0
gyp ERR! node-gyp -v v3.5.0
gyp ERR! not ok
error Command failed with exit code 1.

    at ChildProcess.childProcess.once.code (C:\Users\zoe\AppData\Roaming\npm\node_modules\electron-builder\node_modules\builder-util\src\util.ts:244:14)
    at Object.onceWrapper (events.js:273:13)
    at ChildProcess.emit (events.js:182:13)
    at maybeClose (internal/child_process.js:962:16)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:251:5)
From previous event:
    at runCallback (timers.js:705:18)
    at tryOnImmediate (timers.js:676:5)
    at processImmediate (timers.js:658:5)
From previous event:
    at C:\Users\zoe\AppData\Roaming\npm\node_modules\electron-builder\node_modules\app-builder-lib\src\util\yarn.ts:155:24
From previous event:
    at _rebuild (C:\Users\zoe\AppData\Roaming\npm\node_modules\electron-builder\node_modules\app-builder-lib\src\util\yarn.ts:179:5)
    at rebuild (C:\Users\zoe\AppData\Roaming\npm\node_modules\electron-builder\node_modules\app-builder-lib\out\util\yarn.js:163:19)
    at C:\Users\zoe\AppData\Roaming\npm\node_modules\electron-builder\node_modules\app-builder-lib\src\util\yarn.ts:20:11
    at Generator.next ()
    at runCallback (timers.js:705:18)
    at tryOnImmediate (timers.js:676:5)
    at processImmediate (timers.js:658:5)
From previous event:
    at _installOrRebuild (C:\Users\zoe\AppData\Roaming\npm\node_modules\electron-builder\node_modules\app-builder-lib\out\util\yarn.js:71:28)
    at installOrRebuild (C:\Users\zoe\AppData\Roaming\npm\node_modules\electron-builder\node_modules\app-builder-lib\out\util\yarn.js:55:28)
    at C:\Users\zoe\AppData\Roaming\npm\node_modules\electron-builder\node_modules\app-builder-lib\src\packager.ts:511:13
    at Generator.next ()
From previous event:
    at Packager.installAppDependencies (C:\Users\zoe\AppData\Roaming\npm\node_modules\electron-builder\node_modules\app-builder-lib\src\packager.ts:472:70)
    at C:\Users\zoe\AppData\Roaming\npm\node_modules\electron-builder\node_modules\app-builder-lib\src\packager.ts:420:20
    at Generator.next ()
From previous event:
    at Packager.doBuild (C:\Users\zoe\AppData\Roaming\npm\node_modules\electron-builder\node_modules\app-builder-lib\src\packager.ts:396:24)
    at C:\Users\zoe\AppData\Roaming\npm\node_modules\electron-builder\node_modules\app-builder-lib\src\packager.ts:366:57
    at Generator.next ()
    at C:\Users\zoe\AppData\Roaming\npm\node_modules\electron-builder\node_modules\graceful-fs\graceful-fs.js:111:16
    at C:\Users\zoe\AppData\Roaming\npm\node_modules\electron-builder\node_modules\graceful-fs\graceful-fs.js:45:10
    at FSReqWrap.oncomplete (fs.js:141:20)
From previous event:
    at Packager._build (C:\Users\zoe\AppData\Roaming\npm\node_modules\electron-builder\node_modules\app-builder-lib\src\packager.ts:335:133)
    at C:\Users\zoe\AppData\Roaming\npm\node_modules\electron-builder\node_modules\app-builder-lib\src\packager.ts:331:23
    at Generator.next ()
    at runCallback (timers.js:705:18)
    at tryOnImmediate (timers.js:676:5)
    at processImmediate (timers.js:658:5)
From previous event:
    at Packager.build (C:\Users\zoe\AppData\Roaming\npm\node_modules\electron-builder\node_modules\app-builder-lib\src\packager.ts:288:14)
    at build (C:\Users\zoe\AppData\Roaming\npm\node_modules\electron-builder\node_modules\app-builder-lib\src\index.ts:59:28)
    at build (C:\Users\zoe\AppData\Roaming\npm\node_modules\electron-builder\src\builder.ts:227:10)
    at then (C:\Users\zoe\AppData\Roaming\npm\node_modules\electron-builder\src\cli\cli.ts:46:19)
error Command failed with exit code 1.

然后,electron-rebuild not required if you use electron-builder, please consider t

解决步骤2:
看到了这个,我发现如果使用电子打包,则不需要electron重建,因为devDependencies里写了rebuild,我把它去掉了!
package中写的:

 "devDependencies": {
    "@babel/plugin-proposal-decorators": "^7.4.0",
    "@babel/plugin-transform-react-jsx": "^7.3.0",
    "@babel/plugin-transform-react-jsx-self": "^7.2.0",
    "@babel/plugin-transform-react-jsx-source": "^7.2.0",
    "electron": "^4.1.4",
    "node-sass": "^4.11.0"
  }

最后,解决步骤3:
执行 npm run dist,终于成功了!
好开心·····~~

最后附上我的package.json:

{
  "name": "sqlite",
  "version": "0.1.0",
  "author": "Zoe",
  "private": true,
  "description": "测试打包",
  "dependencies": {
    "@babel/core": "7.2.2",
    "@svgr/webpack": "4.1.0",
    "antd": "^3.16.3",
    "babel-core": "7.0.0-bridge.0",
    "babel-eslint": "9.0.0",
    "babel-jest": "23.6.0",
    "babel-loader": "8.0.5",
    "babel-plugin-named-asset-import": "^0.3.1",
    "babel-preset-react-app": "^7.0.2",
    "bfj": "6.1.1",
    "case-sensitive-paths-webpack-plugin": "2.2.0",
    "colors": "^1.3.3",
    "css-loader": "1.0.0",
    "dotenv": "6.0.0",
    "dotenv-expand": "4.2.0",
    "eslint": "5.12.0",
    "eslint-config-react-app": "^3.0.8",
    "eslint-loader": "2.1.1",
    "eslint-plugin-flowtype": "2.50.1",
    "eslint-plugin-import": "2.14.0",
    "eslint-plugin-jsx-a11y": "6.1.2",
    "eslint-plugin-react": "7.12.4",
    "file-loader": "2.0.0",
    "fs-extra": "^7.0.1",
    "html-webpack-plugin": "4.0.0-alpha.2",
    "identity-obj-proxy": "3.0.0",
    "jest": "23.6.0",
    "jest-pnp-resolver": "1.0.2",
    "jest-resolve": "23.6.0",
    "jest-watch-typeahead": "^0.2.1",
    "mini-css-extract-plugin": "0.5.0",
    "mobx": "^5.9.4",
    "mobx-react": "^5.4.3",
    "optimize-css-assets-webpack-plugin": "5.0.1",
    "ora": "^3.4.0",
    "pcsclite": "^0.6.0",
    "pnp-webpack-plugin": "1.2.1",
    "postcss-flexbugs-fixes": "4.1.0",
    "postcss-loader": "3.0.0",
    "postcss-preset-env": "6.5.0",
    "postcss-safe-parser": "4.0.1",
    "react": "^16.8.6",
    "react-app-polyfill": "^0.2.2",
    "react-dev-utils": "^8.0.0",
    "react-dom": "^16.8.6",
    "react-router-dom": "^5.0.0",
    "resolve": "1.10.0",
    "sass-loader": "7.1.0",
    "spawn-rx": "^3.0.0",
    "sqlite3": "^4.0.6",
    "style-loader": "0.23.1",
    "terser-webpack-plugin": "1.2.2",
    "typescript": "^3.4.4",
    "url-loader": "1.1.2",
    "webpack": "4.28.3",
    "webpack-dev-server": "3.1.14",
    "webpack-manifest-plugin": "2.0.4",
    "workbox-webpack-plugin": "3.6.3",
    "yargs": "^13.2.2"
  },
  "build": {
    "appId": "nfcKey-1",
    "copyright": "Zoe",
    "productName": "NFCKey",
    "asar": false,
    "dmg": {
      "background": "public/background.png",
      "window": {
        "x": 100,
        "y": 100,
        "width": 500,
        "height": 300
      }
    },
    "win": {
      "icon": "public/icon.ico"
    },
    "files": [
      "build/**/*",
      "node_modules/**/*",
      "public/main.js",
      "src/api/**/*"
    ],
    "directories": {
      "buildResources": "assets"
    }
  },
  "main": "public/main.js",
  "scripts": {
    "start": "node scripts/start.js",
    "build": "rm -rf ./build && node scripts/build.js",
    "test": "node scripts/test.js",
    "electron": "electron .",
    "rebuild": "electron-rebuild -f -w sqlite3",
    "pack": "electron-builder --dir",
    "dist": "electron-builder",
    "postinstall": "electron-builder install-app-deps",
    "electron-pack": "rm -rf ./dist &&build -w"
  },
  "homepage": "./",
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": [
    ">0.2%",
    "not dead",
    "not ie <= 11",
    "not op_mini all"
  ],
  "jest": {
    "collectCoverageFrom": [
      "src/**/*.{js,jsx,ts,tsx}",
      "!src/**/*.d.ts"
    ],
    "resolver": "jest-pnp-resolver",
    "setupFiles": [
      "react-app-polyfill/jsdom"
    ],
    "testMatch": [
      "/src/**/__tests__/**/*.{js,jsx,ts,tsx}",
      "/src/**/?(*.)(spec|test).{js,jsx,ts,tsx}"
    ],
    "testEnvironment": "jsdom",
    "testURL": "http://localhost",
    "transform": {
      "^.+\\.(js|jsx|ts|tsx)$": "/node_modules/babel-jest",
      "^.+\\.css$": "/config/jest/cssTransform.js",
      "^(?!.*\\.(js|jsx|ts|tsx|css|json)$)": "/config/jest/fileTransform.js"
    },
    "transformIgnorePatterns": [
      "[/\\\\]node_modules[/\\\\].+\\.(js|jsx|ts|tsx)$",
      "^.+\\.module\\.(css|sass|scss)$"
    ],
    "moduleNameMapper": {
      "^react-native$": "react-native-web",
      "^.+\\.module\\.(css|sass|scss)$": "identity-obj-proxy"
    },
    "moduleFileExtensions": [
      "web.js",
      "js",
      "web.ts",
      "ts",
      "web.tsx",
      "tsx",
      "json",
      "web.jsx",
      "jsx",
      "node"
    ],
    "watchPlugins": [
      "E:\\aPritice\\JieDe\\sqlite\\sqlite\\node_modules\\jest-watch-typeahead\\filename.js",
      "E:\\aPritice\\JieDe\\sqlite\\sqlite\\node_modules\\jest-watch-typeahead\\testname.js"
    ]
  },
  "babel": {
    "presets": [
      "react-app"
    ],
    "plugins": [
      [
        "@babel/plugin-proposal-decorators",
        {
          "legacy": true
        }
      ]
    ]
  },
  "devDependencies": {
    "@babel/plugin-proposal-decorators": "^7.4.0",
    "@babel/plugin-transform-react-jsx": "^7.3.0",
    "@babel/plugin-transform-react-jsx-self": "^7.2.0",
    "@babel/plugin-transform-react-jsx-source": "^7.2.0",
    "electron": "^4.1.4",
    "node-sass": "^4.11.0"
  }
}

之前打包的问题是,打包出来的文件一直在c盘,然后报不能加载本地文件
我的main.js中写的文件地址:刚开始是这个:

win.loadFile(path.join(__dirname, '/build/index.html'))

运行是没问题,但是打包就不行,最后改成了这个:

win.loadURL(`file://${path.join(__dirname, '../build/index.html')}`)

因为我使用的create-react-app创建的工程,所以用的webpack,配置项做了下修改:
electron打包遇到的问题_第9张图片
修改了下package中的信息:
package图片
这个文件的打包出来,就是dist文件夹下的内容,然后不会报本地文件这个错误了

打包完成了,下一步继续踩坑…

==================================================================
如果以上的代码你没有解决问题,你可以看以下的网址:
如果你用到了node-gpy模块,按官方的配置一下https://github.com/nodejs/node-gyp#on-windows

用builder打包出来的页面路径直接引用到了c盘的根目录:https://newsn.net/say/electron-loadfile.html
https://newsn.net/say/electron-gyp-msvs_version.html
https://newsn.net/say/electron-gyp-param.html
https://newsn.net/say/electron-rebuild-env.html

终于好了,收拾东西,肥家··
有哪里写的不对的,麻烦帮忙指正!谢谢!
这篇文章,写给不长心的我,避免下次再犯!

你可能感兴趣的:(bug,electron,react)