NW.js使用nw-builder构建打包报错:Could not find a package.json in your src folder
错误描述:
NW.js应用使用nw-builder构建打包时,报错:The promise rejected with the reason "Could not find a package.json in your src folder".] {code: 'ERR_UNHANDLED_REJECTION'}
具体报错日志如下:
D:\whw\study\nwjs\test2>npm run build
> firstapp@1.0.0 build
> nwbuild src/ --platforms win32,win64,osx64,linux32,linux64 --buildDir dist/
node:internal/process/promises:246
triggerUncaughtException(err, true /* fromPromise */);
^
[UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason "Could not find a package.json in your src folder".] {
code: 'ERR_UNHANDLED_REJECTION'
}
D:\whw\study\nwjs\test2>
解决方法:
从报错上看,是nwbuild找不到package.json文件,根据nw-builder文档修改package.json配置中build命令src目录参数后OK。
nwbuild src/ --platforms win32,win64 --buildDir dist/
修改为:
nwbuild ./src/**/* --platforms win32,win64 --buildDir dist/ --mode=build
完整的package.json内容如下:
{
"name": "helloworld",
"main": "index.html",
"version": "0.0.1",
"description": "first Nw.js Demo",
"scripts": {
"dev": "nw .",
"build": "nwbuild ./src/**/* --platforms win32,win64 --buildDir dist/ --mode=build"
},
"devDependencies": {
"nw": "^0.18.3",
"nw-builder": "^3.1.2"
},
"author": "",
"license": "ISC"
}
修改后即可正常构建打包:
D:\whw\study\nwjs\test2>npm run build
> firstapp@1.0.0 build
> nwbuild ./src/**/* --platforms win32,win64 --buildDir dist/ --mode=build
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 100% | ETA: 0s | 148164619/14816461
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 100% | ETA: 0s | 172396170/17239617
D:\whw\study\nwjs\test2>
参考文档:
nw-builder
https://nwutils.io/nw-builder/getting-started
(完)