首页 > 代码库 > npm 安装相关环境及测试

npm 安装相关环境及测试

0、npm 命令

C:\Users\Carrie>npm express

Usage: npm <command>

where <command> is one of:
    add-user, adduser, apihelp, author, bin, bugs, c, cache,
    completion, config, ddp, dedupe, deprecate, docs, edit,
    explore, faq, find, find-dupes, get, help, help-search,
    home, i, info, init, install, isntall, issues, la, link,
    list, ll, ln, login, ls, outdated, owner, pack, prefix,
    prune, publish, r, rb, rebuild, remove, repo, restart, rm,
    root, run-script, s, se, search, set, show, shrinkwrap,
    star, stars, start, stop, submodule, t, tag, test, tst, un,
    uninstall, unlink, unpublish, unstar, up, update, v,
    version, view, whoami

npm <cmd> -h     quick help on <cmd>
npm -l           display full usage info
npm faq          commonly asked questions
npm help <term>  search for help on <term>
npm help npm     involved overview

1、安装express(全局)

npm install express -g(失败:express -v 报错:express: command not found)

npm install -g express-generator (成功:$ express -V 显示:4.0.0)

2、安装jade

npm install jade

3、安装mysql

npm install mysql

完毕后可以在nodejs\node_modules下看到express(失败的文件夹)/express-generator 、 jade、 mysql 、.bin这4个文件夹,并且.bin中应该有jade 和jade.cmd。

在nodejs下也会出现express.cmd(npm.cmd也在那里)。

 

4、在nodejs文件夹下创建一个项目myapp

express myapp

查看nodejs\myapp下的文件结构

$ ls
app.js  bin  package.json  public  routes  views

 

5、复制node_modules到myapp下面(这个每次建一个项目都需要复制一次吗?)

参见:http://blog.csdn.net/lovesomnus/article/details/22731771

环境搭建到此完工,下面做一个demo测试!

在myapp下新建helloworld.js

var http = require("http");
http.createServer(function(request, response) {  
    response.writeHead(200, {"Content-Type": "text/plain"});  
    response.write("Hello World");  
    response.end();
}).listen(8888);
console.log("nodejs start listen 8888 port!");

Carrie@CARRIE-PC /c/Program Files/nodejs/myapp
$ node helloworld.js
nodejs start listen 8888 port!

打开http://127.0.0.1:8888/

出现:Hello World