日期:2014-05-16  浏览次数:20359 次

Windows系统下nodejs安装及配置
关于nodejs中文站,目前活跃度最好的知识站应该是http://www.cnodejs.org/ ,而http://cnodejs.org/则活跃度较低。Express.js是nodejs的一个MVC开发框架,并且支持jade等多种模板,是Node.js上最流行的Web开发框架。这几天刚接触PhoneGap,以前也看了一些nodejs的基础但苦于时间有限一直没机会亲自搭建一个nodejs环境,今天周末,部署了PhoneGap到Android,顺便一同搭建了一下nodejs本地环境,自己的操作步骤如下:

第1步:下载、安装文件

代开nodejs的官网http://www.nodejs.org/download/ 下载最新版本,下载完成之后,双击 node-v0.10.20-x86.msi,开始安装nodejs,默认是安装在C:\Program Files\nodejs目录。安装好后系统默认的环境变量path是C:\Documents and Settings\Administrator\Application Data\npm;可以根据需要手动指向本地安装目录,如:C:\Program Files\nodejs\node_modules\npm将全局目录设置设为本地初始默认安装目录一致。


第2步:安装相关模块环境


打开C:\Program Files\nodejs目录你会发现里面自带了Npm这个nodejs插件的管理工具,直接用Npm安装相关需要的相关模块即可(其他有些系统可能需要单独安装NPM下载地址https://github.com/isaacs/npm,也可直接用Git工具下载git clone --recursive git://github.com/isaacs/npm.git下载完成后,命令行首先定位到npm包所在目录,输入代码node cli.js install npm -gf 进行安装。)

系统开始菜单--程序--进入node.js command prompt 命令窗口

键入命令:cd C:\Program Files\nodejs 即可进入nodejs 安装目录 C:\Program Files\nodejs

现在开始安装相关模块环境

node模块的安装分为全局模式和本地模式。一般情况下会以本地模式运行,包会被安装到和你的应用代码统计的本地node_modules目录下。在全局模式下,Node包会被安装到Node的默认安装目录下的node_modules下。

第一种方法是键入命令:npm install express 默认安装express的最新版本。若在后面加版本号可安装指定版本,如npm install express@3.0.6 回车开始安装express,安装完成后会在当前目录下的node_modules文件夹下多出express相关的两个文件夹express和.bin。

另一种全局安装方式是键入命令:npm install express -g  ,安装完成命令行会提示 npm info ok。参数-g的含义是代表安装到全局环境里面。如果沒有-g的话会安装到当前node_modules目录下(如无则新建node_modules文件夹)。个人不建议初学者使用这种将包安装到全局环境中的做法,因为在js实例代码中,直接通过require()的方式是没有办法调用全局安装包的,报错 throw err;Error: Cannot find module 'express' ,此时可以将node_modules整个文件夹复制一份到你的项目工程下。全局的安装是供命令行使用的,使用全局方式安装后,用户就可以在命令行中直接运行该组件包支持的命令,好处是可以提高程序的重复利用程度,避免同样的内容存在多份副本。缺点是难以处理不同的版本依赖。这里的require()是node.js内置函数,用来引入其他模块以备代码中调用模块的函数和变量,默认下node.js会在NODE_PATH和目前js所在项目目录下的node_modules文件夹下去寻找模块。因此,如果是全局安装,不复制系统安装node_modules文件夹到项目node_modules文件夹下,还可以选择将环境变量的NODE_PATH设置为C:\Program Files\nodejs,设置完成后再去项目目录运行命令node app.js就不会报错了。express.js继承自connect模块,所以若你的node_modules文件夹下没有connect模块也是不能运行的。
以上为本人的经验总结。后来查阅文档发现国外有更为详细的答复:
援引Marek的问题Error: Cannot find module ‘express’的解答:

This problems seems to be quite popular among Windows users. It seems to occur after node has been reinstalled or updated  or when hidden attribute has been removed from  C:\Users\IMaster\AppData folder.  It might be one of those things that can make you feel bad especially if you don’t wont to apply some quick hacks like:  npm link express

Node returns error because is not able to find required module and that is why problem in most cases is actually easy to fix. First place to check would be require.paths. After typing it in node console I received:
Error: require.paths is removed. Use node_modules folders, or the NODE_PATH environment variable instead.

At the time of writing I am using v0.6.19 but you might see this or similar warning if you using newer version.

As stated  you have 2 choices. You can install express (or another module) to local node_modules directory using npm install express or after installing module globally
    
npm install express -g

you can link it with your current project using
    
npm link express

Second and last option is to create or update  NODE_PATH system variable pointing your node to the right place in the system. If you are Windows user use export command as shown below:
    
export NODE_PATH="C:\Users\IMarek\AppData\Roaming\npm\node_modules"

Now you should update PATH variable as well
    
set PATH=%PATH%;%NODE_PATH%

Try to run your module now.

You should be fine.

安装完成Express后运行node app.js 浏览器输入http://loca