cordova is not a valid task

总结原因:node或npm、ionic版本不匹配。解决方法:node降级 或 ionic 升级。
总结结论:node版本低于@6.0.0会导致淘宝镜像出问题,太高会导致与 ionic 不匹配。
可以 ionic build Android 和 cordova build Android 分开写。
最简单的方法:升级ionic。

问题如下

G:\Work\AMC>ionic cordova build android
no original function chain to wrap
  _             _
 (_)           (_)
  _  ___  _ __  _  ___
 | |/ _ \| '_ \| |/ __|
 | | (_) | | | | | (__
 |_|\___/|_| |_|_|\___|  CLI v1.3.2

Usage: ionic task args

=======================

cordova is not a valid task

Available tasks: (use --help or -h for more info)

   info  ...........  List information about the users runtime environment
   help  ...........  Provides help for a certain command
   start  ..........  Starts a new Ionic project in the specified PATH
   serve  ..........  Start a local development server for app dev/testing
   platform  .......  Add platform target for building an Ionic app
   run  ............  Run an Ionic project on a connected device
   emulate  ........  Emulate an Ionic project on a simulator or emulator
   build  ..........  Locally build an Ionic project for a given platform
   plugin  .........  Add a Cordova plugin
   resources  ......  Automatically create icon and splash screen resources (beta)
   package  ........  Package an app using the Ionic Build service (beta)
   upload  .........  Upload an app to your Ionic account
   lib  ............  Gets Ionic library version or updates the Ionic library
   setup  ..........  Configure the project with a build tool (beta)
   browser  ........  Add another broswer for a platform (beta)
   service  ........  Add an Ionic service package and install any required plugins
   add  ............  Add a bower component to the project
   remove  .........  Remove a bower component from the project
   list  ...........  List bower components from the project

解决方法尝试:1

You need to add Add the platforms that you want to target your app. We will add the 'ios' and 'android' platform.

cordova platform add --save android

结果显示

Using cordova-fetch for cordova-android@^6.1.2
(node:3276) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): CordovaError: Platform android already added.
(node:3276) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

分别依次执行下面两行命令

cordova platform remove android
cordova platform add [email protected]
————操作有效

最终没有解决我的问题


解决方法尝试:2

删除platform文件夹
无效

塞一段题外话:
卸载ionic 和 cordova

卸载方法:执行下面三个语句
npm uninstall -g ionic
npm uninstall -g cordova
npm cache clean

这里有个坑是不要用cnpm卸载, 一定要用npm! cnpm卸载无效!
再重新安装ionic和cordova .


解决方法尝试:3

删除node_modules文件夹
删除方法:cmd下:

npm install rimraf -g
然后进入你的node_modules的上级目录:
rimraf node_modules

解决方法尝试:4

重装node

安装详细步骤
这一步可以完全默认安装下去
说明:新版的Node.js已自带npm,安装Node.js时会一起安装,npm的作用就是对Node.js依赖的包进行管理,也可以理解为用来安装/卸载Node.js需要装的东西。
这里的环境配置主要配置的是npm安装的全局模块所在的路径,以及缓存cache的路径,之所以要配置,是因为以后在执行类似:npm install express [-g] (后面的可选参数-g,g代表global全局安装的意思)的安装语句时,会将安装的模块安装到【C:\Users\用户名\AppData\Roaming\npm】路径中,占C盘空间。
配置环境变量详细过程
配置缓存和安装路径
在安装的文件夹【D:\Nodejs】下创建两个文件夹【node_global】及【node_cache】

cordova is not a valid task_第1张图片
创建文件夹

创建完两个空文件夹之后,打开cmd命令窗口执行

npm config set prefix "D:\Nodejs\node_global"
npm config set cache "D:\Nodejs\node_cache"

设置环境变量
“我的电脑”-右键-“属性”-“高级系统设置”-“高级”-“环境变量”,进入环境变量对话框
在【系统变量】下新建【NODE_PATH】
输入【D:\nodejs\node_global\node_modules】
将【用户变量】下的【Path】修改为【D:\nodejs\node_global】
安装完成

重装ionic

重装 cordova : npm install -g cordova
重装 ionic : npm install -g ionic@版本号
报错,截取一部分报错信息如下:

C:\Users\Administrator>npm install -g [email protected]
D:\Nodejs\node_global\ionic -> D:\Nodejs\node_global\node_modules\ionic\bin\ionic

> [email protected] install D:\Nodejs\node_global\node_modules\ionic\node_modules\
ionic-app-lib\node_modules\node-sass
> node scripts/install.js

Cannot download "https://github.com/sass/node-sass/releases/download/v3.8.0/win3
2-x64-57_binding.node":

HTTP error 404 Not Found

Hint: If github.com is not accessible in your location
      try setting a proxy via HTTP_PROXY, e.g.

      export HTTP_PROXY=http://example.com:1234

or configure npm proxy via

      npm config set proxy http://example.com:8080

gyp verb could not find "python". guessing location
gyp verb ensuring that file exists: C:\Python27\python.exe
gyp ERR! configure error
gyp ERR! stack Error: Can't find Python executable "python", you can set the PYT
HON env variable.
gyp ERR! stack     at failNoPython (D:\Nodejs\node_global\node_modules\ionic\nod
e_modules\ionic-app-lib\node_modules\node-sass\node_modules\node-gyp\lib\configu
re.js:401:14)
gyp ERR! stack     at D:\Nodejs\node_global\node_modules\ionic\node_modules\ioni
c-app-lib\node_modules\node-sass\node_modules\node-gyp\lib\configure.js:356:11
gyp ERR! stack     at FSReqWrap.oncomplete (fs.js:152:21)
gyp ERR! System Windows_NT 6.1.7601
gyp ERR! command "D:\\Nodejs\\node.exe" "D:\\Nodejs\\node_global\\node_modules\\
ionic\\node_modules\\ionic-app-lib\\node_modules\\node-sass\\node_modules\\node-
gyp\\bin\\node-gyp.js" "rebuild" "--verbose" "--libsass_ext=" "--libsass_cflags=
" "--libsass_ldflags=" "--libsass_library="
gyp ERR! cwd D:\Nodejs\node_global\node_modules\ionic\node_modules\ionic-app-lib
\node_modules\node-sass
gyp ERR! node -v v8.9.1
gyp ERR! node-gyp -v v3.3.1
gyp ERR! not ok
Build failed

此处的问题:node-sass网址失效,下载不了,卡住了
解决方法:
使用淘宝镜像安装 ionic

npm install -g cnpm --registry=https://registry.npm.taobao.org
cnpm install -g ionic@版本号

总结原因:node版本号太高。


解决方法尝试:5

管理员权限打开命令行

ionic cordova build android  ----  依旧失败
cordova build android  ---- 成功
ionic build android ---- 失败

错误信息:


Error: Cannot find module 'internal/util/types'
    at Function.Module._resolveFilename (module.js:536:15)
    at Function.Module._load (module.js:466:25)
    at Module.require (module.js:579:17)
    at require (internal/module.js:11:18)
    at evalmachine.:31:26
    at Object. (D:\Nodejs\node_global\node_modules\ionic\node_modules
\ionic-app-lib\node_modules\vinyl-fs\node_modules\graceful-fs\fs.js:11:1)
    at Module._compile (module.js:635:30)
    at Object.Module._extensions..js (module.js:646:10)
    at Module.load (module.js:554:32)
    at tryModuleLoad (module.js:497:12)

Cannot find module 'internal/util/types' (CLI v1.7.16)

Your system information:

Cordova CLI: 8.0.0
Ionic Framework Version: 1.3.4
Ionic CLI Version: 1.7.16
Ionic App Lib Version: 0.7.3
OS: Windows 7 SP1
Node Version: v8.9.1

总结:cordova 没问题,问题出在 ionic 上


解决方法尝试:6

测试:ionic高版本

1.卸载ionic:npm uninstall -g ionic(这里用cnpm卸载是不行的)
2.验证是否卸载成功:ionic -v
3.安装ionic指定版本:npm install -g --save --save-exact [email protected]
4.验证是否安装成功:ionic -v

索性升级到最新版 [email protected]
运行结果:

> ionic-v1 build
[21:12:49] Invoking sass gulp task.
Error in plugin 'sass'
Message:
    scss\ionic.app.scss
Error: File to import not found or unreadable: www/lib/ionic/scss/ionic.
        on line 22 of scss/ionic.app.scss
>> @import "www/lib/ionic/scss/ionic";
   ^

> cordova build android
......
BUILD SUCCESSFUL

有点小问题,不影响打包apk


解决方法尝试:7

重复第四种方法,安装 [email protected],安装淘宝镜像 [email protected] 失败,安装 cnpm@3成功。
切成[email protected]呢,安装淘宝镜像 cnpm 成功,但是cnpm install -g ionic 失败。
安装 [email protected],安装 cnpm 成功,安装 ionic 成功。
ionic cordova build Android ---- 失败
cordova build android ---- 打包执行成功

ionic build android
节选信息如下:

G:\Work\GateWay>ionic build android
 ✗ (node:6260) fs: re-evaluating native module sources is not supported. If you are using the graceful-fs module, please update it to a more recent version.
Running command: D:\Nodejs\node.exe G:\Work\GateWay\hooks\after_prepare\010_add_platform_class.js G:\Work\GateWay
add to body class: platform-android
ANDROID_HOME=D:\Android\SDK
JAVA_HOME=C:\Program Files (x86)\Java\jdk1.8.0_131
Subproject Path: CordovaLib
Observed package id 'extras;m2repository;com;android;support;constraint;constraint-layout-solver;1.0.2' in inconsistent location 'D:\Android\SDK\android-sdk-windows\extras\m2repository\com\android\support\constraint\constraint-la
yout-solver\1.0.2' (Expected 'D:\Android\SDK\extras\m2repository\com\android\support\constraint\constraint-layout-solver\1.0.2')
Observed package id 'extras;m2repository;com;android;support;constraint;constraint-layout;1.0.2' in inconsistent location 'D:\Android\SDK\android-sdk-windows\extras\m2repository\com\android\support\constraint\constraint-layout\1.
0.2' (Expected 'D:\Android\SDK\extras\m2repository\com\android\support\constraint\constraint-layout\1.0.2')
Observed package id 'build-tools;26.0.0-rc2' in inconsistent location 'D:\Android\SDK\build-tools\26.0.0-preview' (Expected 'D:\Android\SDK\build-tools\26.0.0-rc2')
Incremental java compilation is an incubating feature.
.......
BUILD SUCCESSFUL

node@6 环境都会报这个警告,没有太大影响,提示好像是建议升级一下graceful-fs

 ✗ (node:6260) fs: re-evaluating native module sources is not supported. If you are using the graceful-fs module, please update it to a more recent version.

解决方法尝试: npm upgrade -g npm —....— 没有解决问题
解决方法尝试:cnpm install -g [email protected] 进行全局升级。 —....— 没有解决问题
另一种解决方式:node 降级。
不解决也影响不大。

你可能感兴趣的:(cordova is not a valid task)