转到项目目录
mkdir TestProject
cd TestProject
使此目录成为项目的根目录(这将创建一个默认的package.json文件)
npm init --yes
安装所需的npm模块并将其另存为项目依赖项(它将出现在package.json中)
npm install request --save
使用包示例中的代码在项目目录中创建test.js文件
var request = require('request');
request('http://www.google.com', function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body); // Print the google web page.
}
});
您的项目目录应如下所示
TestProject/
- node_modules/
- package.json
- test.js
现在只需在项目目录中运行节点
node test.js