问题
在执行命令cordova create project-name
时失败,错误消息如下:
Creating a new cordova project.
Unhandled "error" event. ( Error from Cordova Fetch: Error: cmd: Command failed with exit code 1 Error output:
npm ERR! code ENOLOCAL
npm ERR! Could not install from "..\AppData\Roaming\npm\node_modules\cordova\node_modules\_cordova-app-hello-world@3.12.0@cordova-app-hello-world\index.js" as it does not contain a package.json file.
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\administrator\AppData\Roaming\npm-cache\_logs\2019-01-11T02_55_02_505Z-debug.log)
解决方法
- 打开文件
C:\Users\你的用户名\AppData\Roaming\npm\node_modules\cordova\node_modules\cordova-lib\node_modules\cordova-create\node_modules\cordova-fetch\index.js
- 查找代码
fetchArgs.push(target);
,然后添加以下代码到这行前面:
if(fs.existsSync(target) && fs.statSync(target).isFile()){
target = require(target).dirname
}
问题包版本:
- cordova: 8.1.2
- cordova-app-hello-world: 3.12.0
- cordova-create: 1.1.2
- cordova-fetch: 1.3.1 其它版本并未进行测试
原因 (胡乱猜的)
打开文件 C:\Users\hyjiacan\AppData\Roaming\npm\node_modules\cordova\node_modules\cordova-lib\node_modules\cordova-create\index.js,查找注释// handle when input wants to specify sub-directory (specified in index.js as "dirname" export);
,看看就知道原因了