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

问题描述 投票:-1回答:1

我正在尝试从终端运行javascript文件prompt.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)

这是我的prompt.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在项目文件夹中运行其他文件。我也可以从另一个项目文件夹中打开来自终端的prompt.js文件,我从中复制了源代码。我不知道为什么会这样?我将非常感谢帮助谢谢。

javascript node.js terminal visual-studio-code
1个回答
0
投票

line var prompt = require('prompt');导致错误。错误是说它找不到所说的模块。或者模块不是项目的一部分,它不存在。解决方案是找到名为prompt的模块,并将该目录添加到项目文件夹中。这将解决问题。

© www.soinside.com 2019 - 2024. All rights reserved.