原本觉得在Mac上搭建vue.js的环境挺简单的,谁知遇到各类问题(多是RP问题),网上解决的方法也寥寥无几,这里就记录下遇到的坑。html
一、安装 brew,这个简单,直接执行远程脚本vue
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
二、安装 nodejs,这一步时间可能略长(执行时间长短也有可能跟网络有关系)java
brew install nodejs
三、获取nodejs模块安装目录访问权限node
sudo chmod -R 777 /usr/local/lib/node_modules/
四、安装淘宝镜像,国内直接使用 npm 的官方镜像是很是慢的,因此这里使用淘宝 NPM 镜像python
1)更改npm源:webpack
npm config set registry https://registry.npm.taobao.org
(2)配置后可经过下面方式来验证是否成功,若是显示“https://registry.npm.taobao.org”则说明配置成功nginx
npm config get registry
(3)而后安装淘宝的cnpm(若是该步骤报错“rollbackFailedOptional”,能够尝试执行步骤4或步骤5,不然跳过步骤4或步骤5)git
npm install -g cnpm --registry=https://registry.npm.taobao.org
(4)先删除原有的全部代理,再从新安装淘宝的cnpmgithub
npm config rm proxy npm config rm https-proxy npm install -g cnpm --registry=https://registry.npm.taobao.org
(5)报错“rollbackFailedOptional”,还多是权限问题,用sudo执行:
sudo npm install -g cnpm --registry=https://registry.npm.taobao.org
如下是安装淘宝cnpm成功的结果:
MacBook-Pro:~ hu$ sudo npm install -g cnpm --registry=https://registry.npm.taobao.org npm WARN deprecated socks@1.1.10: If using 2.x branch, please upgrade to at least 2.1.6 to avoid a serious bug with socket data flow and an import issue introduced in 2.1.0 /usr/local/bin/cnpm -> /usr/local/lib/node_modules/cnpm/bin/cnpm + cnpm@6.0.0 added 632 packages from 843 contributors in 22.264s
五、用淘宝的cnpm安装vue
cnpm install vue cnpm install --global vue-cli
到这里vue.js环境就算ok了。
一、本身建立并进入一个项目目录,建立一个名为VueDemo的vue项目
cd /usr/local/projects/vue/ vue init webpack VueDemo
建立项目可能会报错“vue-cli · Failed to download repo vuejs-templates/webpack: tunneling socket could not be established, cause=Parse Error”,能够尝试以下: (1)清空npm代理,从新执行
npm config set proxy null vue init webpack VueDemo
(2)或者sudo执行(webpack是构建工具,也就是整个项目是基于webpack的)
sudo vue init webpack VueDemo
建立项目成功的结果:
? Project name VueDemo ? Project description A Vue.js project ? Author danny <danny@gmail.com> ? Vue build standalone ? Install vue-router? Yes ? Use ESLint to lint your code? Yes ? Pick an ESLint preset Standard ? Set up unit tests Yes ? Pick a test runner jest ? Setup e2e tests with Nightwatch? Yes ? Should we run `npm install` for you after the project has been created? (recommended) no vue-cli · Generated "VueDemo". # Project initialization finished! # ======================== To get started: cd vue-demo-01 npm install (or if using yarn: yarn) npm run lint -- --fix (or for yarn: yarn run lint --fix) npm run dev Documentation can be found at https://vuejs-templates.github.io/webpack
二、启动项目 (1)安装项目依赖,启动项目须要先安装项目所需依赖,就跟java的maven项目须要先更新dependencies同样,具体项目都依赖了什么,在项目根目录下package.json中的devDependencies标签下能够看到
cd /usr/local/projects/vue/VueDemo sudo cnpm install
在Mac下,有些项目执行install时可能会报错“libtool: unrecognized option `-static’”,解决方法:在~/.bash_profile中添加“PATH="/Library/Developer/CommandLineTools/usr/bin:$PATH”,再从新打开一个终端,从新运行install命令。
安装成功以后,项目根目录会多出一个node_modules文件夹,这里边就是项目须要的依赖包资源(文件挺多的)。
(2)运行项目,用热加载的方式启动项目,在修改完代码后不用手动刷新浏览器就能实时看到修改后的效果。
cnpm run dev
启动成功的结果:
> vue-demo-01@1.0.0 dev /usr/local/projects/vue/VueDemo > webpack-dev-server --inline --progress --config build/webpack.dev.conf.js 95% emitting DONE Compiled successfully in 3084ms 下午10:58:20 I Your application is running here: http://localhost:8080
打开http://localhost:8080就是vue默认的模板
对于用惯了idea的同窗可能想在idea中开发vue,idea配置vue开发环境网上有不少教程,这里就不重复了,随便贴一篇:https://www.cnblogs.com/phpdragon/p/7216994.html。
当vue.js项目开发完成须要部署时,先打包,再部署。 一、打包 在项目目录下,执行
cnpm run build
执行完以后,项目根目录会出现一个dist文件夹,里面有一个index.html,直接打开就能够看到页面效果。
二、部署 上面步骤,dist就是打好的包,能够直接把dist部署在nginx等服务器下,以nginx为例,把nginx.conf中的location指向dist文件夹,就能够了。
server { listen 80; server_name 127.0.0.1; location / { root /data/delploy/dist/; index index.html index.htm; } }
安装brew
git clone https://github.com/creationix/nvm.git ~/.nvm && cd ~/.nvm && git checkout `git describe --abbrev=0 --tags`
若是报错就切换到指定的文件夹下进行操做
# 报错信息 curl: (7) Failed to connect to raw.githubusercontent.com port 443: Connection
报错版本不一致:
vue create is a Vue CLI 3 only command and you are using Vue CLI 2.9.6. You may want to run the following to upgrade to Vue CLI 3: npm uninstall -g vue-cli npm install -g @vue/cli # 使用 sudo+npm uninstall -g vue-cli sudo + npm install -g @vue/cli 分别执行