node.js: cannot find module 'request'

后端 未结 8 1836
再見小時候
再見小時候 2020-12-07 14:29

I installed request module, and getting the error:

module.js:340
    throw err;
          ^
Error: Cannot find module \'request\'

i\'ve read

8条回答
  •  我在风中等你
    2020-12-07 14:38

    Go to directory of your project

    mkdir TestProject
    cd TestProject
    

    Make this directory a root of your project (this will create a default package.json file)

    npm init --yes
    

    Install required npm module and save it as a project dependency (it will appear in package.json)

    npm install request --save
    

    Create a test.js file in project directory with code from package example

    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.
      }
    });
    

    Your project directory should look like this

    TestProject/
    - node_modules/
    - package.json
    - test.js
    

    Now just run node inside your project directory

    node test.js
    

提交回复
热议问题