首页 > 代码库 > 如何使用Grunt脚手架快速创建Node.js项目

如何使用Grunt脚手架快速创建Node.js项目

  作者:zhanhailiang 日期:2014-11-17

1. 安装node.js,npm工具(略)。

2. 安装grunt, grunt-init

npm install -g grunt-cli
npm install -g grunt-init

3. 下载脚手架:grunt-init-node - Create a Node.js module, including Nodeunit unit tests.

git clone git@github.com:billfeller/grunt-init-node.git ~/.grunt-init/node

注意,其默认路径为~/.grunt-init/xxxx,如果下载到默认路径,后续只需要通过grunt-init xxxx就可直接初始化;若下载到私人指定路径时,则grunt-init /path/to/template使用绝对路径来初始化。

4. 初始化:

[root@~/wade/nodejs/grunt-init-node-demo]# grunt-init node
Running "init:node" (init) task
This task will create one or more files in the current directory, based on the
environment and the answers to a few questions. Note that answering "?" to any
question will show question-specific help and answering "none" to most questions
will leave its value blank.
 
"node" template notes:
Project name shouldn‘t contain "node" or "js" and should be a unique ID not
already in use at search.npmjs.org.
 
Please answer the following:
[?] Project name (grunt-init-node-demo) 
[?] Description (The best project ever.) 
[?] Version (0.1.0) 
[?] Project git repository (git://github.com/billfeller/grunt-init-node-demo.git) 
[?] Project homepage (https://github.com/billfeller/grunt-init-node-demo) 
[?] Project issues tracker (https://github.com/billfeller/grunt-init-node-demo/issues) 
[?] Licenses (MIT) 
[?] Author name (billfeller) 
[?] Author email (billfeller@gmail.com) 
[?] Author url (none) http://blog.csdn.net/billfeller
[?] What versions of node does it run on? (>= 0.8.0) 
[?] Main module/entry point (lib/grunt-init-node-demo) 
[?] Npm test command (grunt nodeunit) 
[?] Will this project be tested with Travis CI? (Y/n) Y
[?] Do you need to make any changes to the above before continuing? (y/N) N
 
Writing .gitignore...OK
Writing .jshintrc...OK
Writing .travis.yml...OK
Writing Gruntfile.js...OK
Writing README.md...OK
Writing lib/grunt-init-node-demo.js...OK
Writing test/grunt-init-node-demo_test.js...OK
Writing LICENSE-MIT...OK
Writing package.json...OK
 
Initialized from template "node".
You should now install project dependencies with npm install. After that, you
may execute project tasks with grunt. For more information about installing
and configuring Grunt, please see the Getting Started guide:
 
http://gruntjs.com/getting-started
 
Done, without errors.

当看到“Done, without errors.”,就表示初始化成功,之后就可以开始专注于业务开发了。

注:如果在初始化时遇到如下提示:

[root@~/wade/nodejs]# grunt-init node
Running "init:node" (init) task
This task will create one or more files in the current directory, based on the
environment and the answers to a few questions. Note that answering "?" to any
question will show question-specific help and answering "none" to most questions
will leave its value blank.

Warning: Existing files may be overwritten! Use --force to continue.

Aborted due to warnings.

那是因为当前目录已经存在同名的相关文件,此时可以通过清除相关文件或者重建一个空目录来初始化。

5. grunt官方维护几个模板:

  • grunt-init-commonjs - Create a commonjs module, including Nodeunit unit tests.
  • grunt-init-gruntfile - Create a basic Gruntfile.
  • grunt-init-gruntplugin - Create a Grunt plugin, including Nodeunit unit tests.
  • grunt-init-jquery - Create a jQuery plugin, including QUnit unit tests.
  • grunt-init-node - Create a Node.js module, including Nodeunit unit tests.

可以参考以上做法初始化相关的项目。

6. 参考:

  • Getting started
  • Project Scaffolding
  • 利用 Grunt (几乎)无痛地做前端开发 (一)

如何使用Grunt脚手架快速创建Node.js项目