开发环境搭建

taro-cli

系统:macOS 10.14.2,nvm 0.33,node 版本 v8.12.0 LTS。

安装 taro-cli

  1. npm install -g @tarojs/cli

使用 taro-cli 初始化 taro 项目

注意:$后面的是需要写的,taro-template是项目名(本行写到这即可)

  1. [fengyfei-2:labs a1$ taro init taro-template
  2. 👽 Taro v1.2.14
  3. Taro即将创建一个新项目!
  4. Need help? Go and open issue: https://github.com/NervJS/taro/issues/new
  5. ? 请输入项目介绍! Template with Taro UI
  6. ? 是否需要使用 TypeScript No
  7. ? 请选择 CSS 预处理器(Sass/Less/Stylus Sass
  8. ? 请选择模板 Redux 模板
  9. 创建项目: taro-template
  10. 创建配置目录: taro-template/config
  11. 创建源码目录: taro-template/src
  12. 创建页面目录: taro-template/src/pages
  13. 创建 actions 目录: taro-template/src/actions
  14. 创建 constants 目录: taro-template/src/constants
  15. 创建 reducers 目录: taro-template/src/reducers
  16. 创建 store 目录: taro-template/src/store
  17. 创建页面 JS 文件: taro-template/src/pages/index/index.js
  18. 创建 action counter JS 文件: taro-template/src/actions/counter.js
  19. 创建 constant counter JS 文件: taro-template/src/constants/counter.js
  20. 创建 reducer counter JS 文件: taro-template/src/reducers/counter.js
  21. 创建 reducer 入口 JS 文件: taro-template/src/reducers/index.js
  22. 创建 store JS 文件: taro-template/src/store/index.js
  23. 创建页面 SCSS 文件: taro-template/src/pages/index/index.scss
  24. 创建文件: taro-template/src/app.js
  25. 创建文件: taro-template/src/app.scss
  26. 创建文件: taro-template/src/index.html
  27. 创建文件: taro-template/config/index.js
  28. 创建文件: taro-template/config/dev.js
  29. 创建文件: taro-template/config/prod.js
  30. 创建文件: taro-template/.editorconfig
  31. 创建文件: taro-template/.gitignore
  32. 创建文件: taro-template/package.json
  33. 创建文件: taro-template/.eslintrc
  34. 创建文件: taro-template/project.config.json
  35. Initialized empty Git repository in /Users/fengyfei/Github/Go/src/github.com/fengyfei/labs/taro-template/.git/
  36. 安装成功
  37. warning kleur@2.0.2: Please upgrade to kleur@3 or migrate to 'ansi-colors' if you prefer the old syntax. Visit <https://github.com/lukeed/kleur/releases/tag/v3.0.0\> for migration path(s).
  38. warning circular-json@0.3.3: CircularJSON is in maintenance only, flatted is its successor.
  39. yarn install v1.1.0
  40. info No lockfile found.
  41. [1/4] Resolving packages...
  42. [2/4] Fetching packages...
  43. [3/4] Linking dependencies...
  44. [4/4] Building fresh packages...
  45. success Saved lockfile.
  46. Done in 171.32s.
  47. 创建项目 taro-template 成功!
  48. 请进入项目目录 taro-template 开始工作吧!😝

taro-ui

进入创建的项目目录(一般taro-template创建在二级目录下,一级目录是本机带有的,这时候进入目录是 cd template/ 或者 cd ~/taro-template/。如果在三级目录,需要先找二级目录 比如 cd ~/github/,再找三级目录: cd ~template),执行:

  1. npm i taro-ui -S

注意;如果本步失败,可能是少什么包,执行 npm i (把包全都装一遍),再次执行 npm i taro-ui -S

并在项目目录下 src/app.scss 导入全局配置文件

  1. @import "~taro-ui/dist/style/index.scss";

运行小程序

在项目目录下执行如下命令

  1. taro build --type weapp --watch

由于版本问题,可能会缺少依赖。缺少依赖需要安装

  1. npm install @tarojs/plugin-csso --save-dev
  2. taro build --type weapp --watch

环境更改需要执行 npm rebuild npde-sass

  1. This usually happens because your environment has changed since running `npm install`.
  2. Run `npm rebuild node-sass` to download the binding for your current environment.
  3. $ npm rebuild node-sass
  4. $ taro build --type weapp --watch

扩充

  1. 1s-MacBook-Air-5:samplate a1$ pwd
  2. /Users/a1/github/samplate
  3. 1s-MacBook-Air-5:samplate a1$ cd ../
  4. 1s-MacBook-Air-5:github a1$ ls -la
  5. total 32
  6. drwxr-xr-x 23 a1 staff 736 3 25 14:42 .
  7. drwxr-xr-x+ 84 a1 staff 2688 3 23 17:53 ..
  8. -rw-r--r--@ 1 a1 staff 14340 3 24 11:28 .DS_Store
  9. drwxr-xr-x 6 a1 staff 192 4 18 2018 .idea
  10. drwxr-xr-x 3 a1 staff 96 11 28 2017 .vscode
  11. drwxr-xr-x 43 a1 staff 1376 3 3 2018 GO
  12. drwxr-xr-x 5 a1 staff 160 6 1 2018 community-data-statistics
  13. drwxr-xr-x 6 a1 staff 192 3 26 2018 gitTest
  14. drwxr-xr-x 21 a1 staff 672 9 18 2018 gopackage
  15. drwxr-xr-x 16 a1 staff 512 6 17 2018 gosnippet
  16. drwxr-xr-x 3 a1 staff 96 4 18 2018 gostudy
  17. drwxr-xr-x 9 a1 staff 288 6 21 2018 graduation-project
  18. drwxr-xr-x 5 a1 staff 160 6 14 2018 issue-community
  19. drwxr-xr-x 7 a1 staff 224 11 12 15:51 java
  20. drwxr-xr-x 44 a1 staff 1408 11 27 2017 js-study
  21. drwxr-xr-x 9 a1 staff 288 11 17 2017 mini-apps
  22. drwxr-xr-x 24 a1 staff 768 2 16 12:39 nvm
  23. drwxr-xr-x 10 a1 staff 320 8 9 2018 picture
  24. drwxr-xr-x 13 a1 staff 416 3 25 14:52 samplate
  25. drwxr-xr-x 16 a1 staff 512 9 18 2018 shiruitao.github.io
  26. drwxr-xr-x 15 a1 staff 480 3 22 19:47 taro-template
  27. drwxr-xr-x@ 11 a1 staff 352 8 9 2018 wxapp-mall
  28. drwxr-xr-x 14 a1 staff 448 3 12 18:46 yqlm
  29. 1s-MacBook-Air-5:github a1$ ls -la samplate/
  30. total 48
  31. drwxr-xr-x 13 a1 staff 416 3 25 14:52 .
  32. drwxr-xr-x 23 a1 staff 736 3 25 14:42 ..
  33. -rw-r--r-- 1 a1 staff 197 3 25 14:42 .editorconfig
  34. -rw-r--r-- 1 a1 staff 221 3 25 14:42 .eslintrc
  35. drwxr-xr-x 9 a1 staff 288 3 25 14:42 .git
  36. -rw-r--r-- 1 a1 staff 47 3 25 14:42 .gitignore
  37. -rw-r--r-- 1 a1 staff 166 3 25 14:42 .npmrc
  38. drwxr-xr-x 5 a1 staff 160 3 25 14:42 config
  39. drwxr-xr-x 4 a1 staff 128 3 25 14:54 dist
  40. drwxr-xr-x 1357 a1 staff 43424 3 25 14:52 node_modules
  41. -rw-r--r-- 1 a1 staff 2014 3 25 14:52 package.json
  42. -rw-r--r-- 1 a1 staff 271 3 25 14:53 project.config.json
  43. drwxr-xr-x 10 a1 staff 320 3 25 14:42 src
  44. 1s-MacBook-Air-5:github a1$ cd samplate
  45. 1s-MacBook-Air-5:samplate a1$ ls
  46. config dist node_modules package.json project.config.json src
  47. 1s-MacBook-Air-5:samplate a1$ ls -la
  48. total 48
  49. drwxr-xr-x 13 a1 staff 416 3 25 14:52 .
  50. drwxr-xr-x 23 a1 staff 736 3 25 14:42 ..
  51. -rw-r--r-- 1 a1 staff 197 3 25 14:42 .editorconfig
  52. -rw-r--r-- 1 a1 staff 221 3 25 14:42 .eslintrc
  53. drwxr-xr-x 9 a1 staff 288 3 25 14:42 .git
  54. -rw-r--r-- 1 a1 staff 47 3 25 14:42 .gitignore
  55. -rw-r--r-- 1 a1 staff 166 3 25 14:42 .npmrc
  56. drwxr-xr-x 5 a1 staff 160 3 25 14:42 config
  57. drwxr-xr-x 4 a1 staff 128 3 25 14:54 dist
  58. drwxr-xr-x 1357 a1 staff 43424 3 25 14:52 node_modules
  59. -rw-r--r-- 1 a1 staff 2014 3 25 14:52 package.json
  60. -rw-r--r-- 1 a1 staff 271 3 25 14:53 project.config.json
  61. drwxr-xr-x 10 a1 staff 320 3 25 14:42 src
  62. 1s-MacBook-Air-5:samplate a1$ rm -rf *
  63. 1s-MacBook-Air-5:samplate a1$ ls -l
  64. 1s-MacBook-Air-5:samplate a1$ ls -la
  65. total 32
  66. drwxr-xr-x 7 a1 staff 224 3 25 15:01 .
  67. drwxr-xr-x 23 a1 staff 736 3 25 14:42 ..
  68. -rw-r--r-- 1 a1 staff 197 3 25 14:42 .editorconfig
  69. -rw-r--r-- 1 a1 staff 221 3 25 14:42 .eslintrc
  70. drwxr-xr-x 9 a1 staff 288 3 25 14:42 .git
  71. -rw-r--r-- 1 a1 staff 47 3 25 14:42 .gitignore
  72. -rw-r--r-- 1 a1 staff 166 3 25 14:42 .npmrc
  73. 1s-MacBook-Air-5:samplate a1$ rm -f .editorconfig .eslintrc .npmrc
  74. 1s-MacBook-Air-5:samplate a1$ cd ../
  75. 1s-MacBook-Air-5:github a1$ taro init samplate
  76. 👽 Taro v1.2.21
  77. Taro即将创建一个新项目!
  78. Need help? Go and open issue: https://github.com/NervJS/taro/issues/new
  79. ? 当前目录已经存在同名项目,请换一个项目名! 1s-MacBook-Air-5:github a1$
  80. 1s-MacBook-Air-5:github a1$
  81. 1s-MacBook-Air-5:github a1$
  82. 1s-MacBook-Air-5:github a1$
  83. 1s-MacBook-Air-5:github a1$ ls -la
  84. total 32
  85. drwxr-xr-x 23 a1 staff 736 3 25 14:42 .
  86. drwxr-xr-x+ 84 a1 staff 2688 3 23 17:53 ..
  87. -rw-r--r--@ 1 a1 staff 14340 3 24 11:28 .DS_Store
  88. drwxr-xr-x 6 a1 staff 192 4 18 2018 .idea
  89. drwxr-xr-x 3 a1 staff 96 11 28 2017 .vscode
  90. drwxr-xr-x 43 a1 staff 1376 3 3 2018 GO
  91. drwxr-xr-x 5 a1 staff 160 6 1 2018 community-data-statistics
  92. drwxr-xr-x 6 a1 staff 192 3 26 2018 gitTest
  93. drwxr-xr-x 21 a1 staff 672 9 18 2018 gopackage
  94. drwxr-xr-x 16 a1 staff 512 6 17 2018 gosnippet
  95. drwxr-xr-x 3 a1 staff 96 4 18 2018 gostudy
  96. drwxr-xr-x 9 a1 staff 288 6 21 2018 graduation-project
  97. drwxr-xr-x 5 a1 staff 160 6 14 2018 issue-community
  98. drwxr-xr-x 7 a1 staff 224 11 12 15:51 java
  99. drwxr-xr-x 44 a1 staff 1408 11 27 2017 js-study
  100. drwxr-xr-x 9 a1 staff 288 11 17 2017 mini-apps
  101. drwxr-xr-x 24 a1 staff 768 2 16 12:39 nvm
  102. drwxr-xr-x 10 a1 staff 320 8 9 2018 picture
  103. drwxr-xr-x 4 a1 staff 128 3 25 15:01 samplate
  104. drwxr-xr-x 16 a1 staff 512 9 18 2018 shiruitao.github.io
  105. drwxr-xr-x 15 a1 staff 480 3 22 19:47 taro-template
  106. drwxr-xr-x@ 11 a1 staff 352 8 9 2018 wxapp-mall
  107. drwxr-xr-x 14 a1 staff 448 3 12 18:46 yqlm
  108. 1s-MacBook-Air-5:github a1$ rm -rf samplate
  109. 1s-MacBook-Air-5:github a1$
  110. 1s-MacBook-Air-5:github a1$
  111. 1s-MacBook-Air-5:github a1$
  112. 1s-MacBook-Air-5:github a1$ sudo npm i taro-ui -S
  113. Password:
  114. Sorry, try again.
  115. Password:
  116. sudo: 1 incorrect password attempt
  117. 1s-MacBook-Air-5:github a1$ sudo npm install -g @tarojs/cli
  118. Password:
  119. 1s-MacBook-Air-5:github a1$ npm install -g @tarojs/cli
  1. npm -v
  2. node -v
  3. vi /Users/a1/.npm/_logs/2019-03-25T12_19_59_370Z-debug.log
  4. ls -la /Users/a1/github/samplate/node_modules/_@tarojs_async-await@1.2.21@@tarojs/async-await/node_modules/
  5. ls -la /Users/a1/github/samplate/node_modules/_@tarojs_async-await@1.2.21@@tarojs/async-await/node_module
  6. ls -la /Users/a1/github/samplate/node_modules/_@tarojs_async-await@1.2.21@@tarojs/async-await/node_modules
  7. ls -la /Users/a1/github/samplate/node_modules/_@tarojs_async-await@1.2.21@@tarojs/async-await/node_modules
  8. npm i
  9. cat package.json
  10. :q
  11. ls
  12. cat project.config.json
  13. ls -la config/
  14. ls -la config/index.js
  15. cat config/index.js
  16. cat ~/.npmrc
  17. history | grep npm
  18. cnpm i
  19. sudo rm -rf node_modules
  20. cnpm i
  1. make: *** [Release/obj.target/fse/fsevents.o] Error 1
  2. gyp ERR! build error
  3. gyp ERR! stack Error: `make` failed with exit code: 2
  4. gyp ERR! stack at ChildProcess.onExit (/Users/a1/.nvm/versions/node/v10.15.3/lib/node_modules/npm/node_modules/node-gyp/lib/build.js:262:23)
  5. gyp ERR! stack at ChildProcess.emit (events.js:189:13)
  6. gyp ERR! stack at Process.ChildProcess._handle.onexit (internal/child_process.js:248:12)
  7. gyp ERR! System Darwin 17.7.0
  8. gyp ERR! command "/Users/a1/.nvm/versions/node/v10.15.3/bin/node" "/Users/a1/.nvm/versions/node/v10.15.3/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "build" "--fallback-to-build" "--module=/Users/a1/github/Libplate/node_modules/fsevents/lib/binding/Release/node-v64-darwin-x64/fse.node" "--module_name=fse" "--module_path=/Users/a1/github/Libplate/node_modules/fsevents/lib/binding/Release/node-v64-darwin-x64" "--napi_version=3" "--node_abi_napi=napi"
  9. gyp ERR! cwd /Users/a1/github/Libplate/node_modules/fsevents
  10. gyp ERR! node -v v10.15.3
  11. gyp ERR! node-gyp -v v3.8.0
  12. gyp ERR! not ok
  13. node-pre-gyp ERR! build error
  14. node-pre-gyp ERR! stack Error: Failed to execute '/Users/a1/.nvm/versions/node/v10.15.3/bin/node /Users/a1/.nvm/versions/node/v10.15.3/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js build --fallback-to-build --module=/Users/a1/github/Libplate/node_modules/fsevents/lib/binding/Release/node-v64-darwin-x64/fse.node --module_name=fse --module_path=/Users/a1/github/Libplate/node_modules/fsevents/lib/binding/Release/node-v64-darwin-x64 --napi_version=3 --node_abi_napi=napi' (1)
  15. node-pre-gyp ERR! stack at ChildProcess.<anonymous> (/Users/a1/github/Libplate/node_modules/fsevents/node_modules/node-pre-gyp/lib/util/compile.js:83:29)
  16. node-pre-gyp ERR! stack at ChildProcess.emit (events.js:189:13)
  17. node-pre-gyp ERR! stack at maybeClose (internal/child_process.js:970:16)
  18. node-pre-gyp ERR! stack at Process.ChildProcess._handle.onexit (internal/child_process.js:259:5)
  19. node-pre-gyp ERR! System Darwin 17.7.0
  20. node-pre-gyp ERR! command "/Users/a1/.nvm/versions/node/v10.15.3/bin/node" "/Users/a1/github/Libplate/node_modules/fsevents/node_modules/node-pre-gyp/bin/node-pre-gyp" "install" "--fallback-to-build"
  21. node-pre-gyp ERR! cwd /Users/a1/github/Libplate/node_modules/fsevents
  22. node-pre-gyp ERR! node -v v10.15.3
  23. node-pre-gyp ERR! node-pre-gyp -v v0.10.3
  24. node-pre-gyp ERR! not ok