尝试从终端运行JavaScript程序时出错

时间:2019-02-27 22:21:20

标签: javascript node.js terminal visual-studio-code

我正在尝试从终端运行javascript文件hint.js,但出现此错误。

$ node prompt.js
internal/modules/cjs/loader.js:583
    throw err;
    ^

Error: Cannot find module 'prompt'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:581:15)
    at Function.Module._load (internal/modules/cjs/loader.js:507:25)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    at require (internal/modules/cjs/helpers.js:22:18)
    at Object.<anonymous> (/Users/pathwang/Documents/School/Spring2019/CSC404/CSC404_MyHomework/Patrick_Hwang_CSC404_Hw1/prompt.js:1:77)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)

这是我的alert.js文件中的内容:

 var prompt = require('prompt');
 var schema = {
    properties: {
      name: {
        pattern: /^[a-zA-Z\s\-]+$/,
        message: 'Name must be only letters, spaces, or dashes',
        required: true
      },
      email: {

      },
      exam1: { type: 'number'},
      exam2: { type: 'number'},
      exam3: { type: 'number'}
    }
  };

  var student = {
    name: '',
    id: '',
    exam1: 60,
    exam2: 60,
    exam3: 60,
    at1: true,
    at2: true,
    at3: true,
    at4: true,
    at5: true,
    adjust: true,
    final: 100,
    letter: ' '
  };

  //
  // Get two properties from the user: username and email
  //
  prompt.get(schema, function (err, result) {
    //
    // Log the results.
    //
    console.log('Command-line input received:');
    console.log('  username: ' + result.username);
    console.log('  email: ' + result.email);
    console.log('  exam1:', result.exam1);
    console.log('  exam2:', result.exam2);
    console.log('  exam3:', result.exam3);
    var totalExam;

    totalExam = (1/3) * (result.exam1 + 
                         result.exam2 + 
                         result.exam3);
    console.log(' Average exam =', totalExam )

  });

 //
  // Start the prompt
  //
  prompt.start();

我在正确的目录中,并且可以使用$ node test.js运行项目文件夹中的其他文件。我还可以从终端中的另一个项目文件夹(从中复制源代码)中打开hint.js文件。我不知道为什么会这样?感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

var提示符提示符= require('prompt');导致错误。错误是说找不到该模块。或者该模块不属于项目,因此不存在。解决方案是找到名为提示的模块,并将目录添加到项目文件夹中。这样可以解决问题。