搭建cordova环境需要以下几个步骤:
安装nodejs
使用如下命令查看本机是否已经安装了nodejs:
node -v
如果没有安装,可从官网上下载pkg包直接进行安装:
官网: http://nodejs.org/
点击【install】,会下载mac的安装包。正常安装即可安装cordova
在控制台输入:
sudo npm install -g cordova
如果看到如下结果表示安装成功:
npm WARN deprecated hoek@2.16.3: The major version is no longer supported. Please update to 4.x or newer
npm WARN notice [SECURITY] lodash has the following vulnerability: 1 low. Go here for more details: https://nodesecurity.io/advisories?search=lodash&version=3.10.1 - Run `npm i npm@latest -g` to upgrade your npm version, and then `npm audit` to get more info.
WARN notice [SECURITY] tunnel-agent has the following vulnerability: 1 moderate. Go here for more details: https://nodesecurity.io/advisories?search=tunnel-agent&version=0.4.3 - Run `npm i npm@latest -g` to upgrade your npm version, and then `npm audit` to get more info.
WARN notice [SECURITY] hoek has the following vulnerability: 1 moderate. Go here for more details: https://nodesecurity.io/advisories?search=hoek&version=2.16.3 - Run `npm i npm@latest -g` to upgrade your npm version, and then `npm audit` to get more info.
/usr/local/bin/cordova -> /usr/local/lib/node_modules/cordova/bin/cordova
+ cordova@8.0.0
added 415 packages in 51.37s
创建cordova项目
先使用cd命令进入到目录下,我这里cd到的目录是Cordova,进入目录之后使用命令:
cordova create hello com.mydomain.hello HelloWorld
命令解释:第一个参数是文件目录,第二个参数是app id, 第三个参数是显示的title因为cordovar是跨平台的所以需要为项目安装平台模块,这里只增加android,添加ios平台类似,在控制台操作如下:
cd hello
cordova platform add android
创建成功时,目录中platform中会有一个android的项目如图所示:
其中创建cordova有可能出现的错误如下:
- 解决办法:
在控制台输入如下命令:
sudo chown -R USER: U S E R : GROUP ~/.npm
sudo chown -R USER: U S E R : GROUP ~/.config
之后再输入创建cordovar项目即可。