nodejs 初入
nodejs 模块路径
1.内置模块
如果传递给require函数的是NodeJS内置模块名称,不做路径解析,直接返回内部模块的导出对象,例:require('http')。
2. nodejs 支持(/)或盘符(C:)开头的绝对路径,也支持./开头的相对路径。
3.nodejs还支持第三种路径 模块路径 node_modules ,写法类似foo/bar , 加载的可以使文件也可以是包,主要是用来存放npm模块。
例:在某个模块的绝对路径是/home/user/app.js,在该模块中使用require('foo/bar')方式加载模块时,则NodeJS依次尝试使用以下路径
/home/user/node_modules/foo/bar //当前目录下的node_modules /home/node_modules/foo/bar //父目录下的node_modules my /node_modules/foo/bar //沿路径向上逐级递归,知道根目录下的 node_modules 目录
4.NODE_PATH环境变量
NodeJS允许通过NODE_PATH环境变量来指定额外的模块搜索路径, 一般情况下,自定义配置的时候 ,node_path指向 npm设置的全局安装的目录下的node_modules
例:
NODE_PATH=/home/user/lib:/home/lib
windows 使用分好 linux使用顿号
当使用require('foo/bar')的方式加载模块时,则NodeJS依次尝试以下路径。
/home/user/lib/foo/bar /home/lib/foo/bar
Nodejs 调试
安装不用说了
npm install -g node-inspector
注意的是
node --debug=5858 Node app.js node --debug-brk=5858 app.js
与前端调试相同,打开console面板,提供了一个交互式运行环境——REPL,可以再其中执行变量值得观察,写入测试js代码。
这非常有用 相比watch。

浙公网安备 33010602011771号