简体   繁体   English

Node.js“错误:找不到模块”

[英]Node.js “Error: Can not find module”

I am doing the node.js lesson from lynda.com and am unable to find what is causing the "Error: Can not find module". 我正在上lynda.com上的node.js课程,无法找到导致“错误:找不到模块”的原因。 From everything I can tell the package.json has the flight module in it. 从所有内容我都可以看出package.json中有flight模块。

Everything has been working so far so I know node.js is installed and running correctly. 到目前为止,一切都已经正常进行,所以我知道node.js已安装并正在正确运行。 The way I am running this file is by typing "node app.js" at the command line. 我运行此文件的方式是在命令行中键入“ node app.js”。 Also all of the files are in the same folder. 此外,所有文件都在同一文件夹中。

Here is the console log: 这是控制台日志:

C:\Users\Jonathan\Desktop\flight>node app.js

module.js:340
    throw err;
          ^
Error: Cannot find module './flight'
    at Function.Module._resolveFilename (module.js:338:15)
    at Function.Module._load (module.js:280:25)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.<anonymous> (C:\Users\Jonathan\Desktop\flight\app.js:1:76)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)

C:\Users\Jonathan\Desktop\flight>

Here is the code. 这是代码。

Package.json file Package.json文件

{
  "name": "flight",
  "version": "1.0.0",
  "description": "a module for keeping track of a flight",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "JBaxter",
  "license": "ISC"
}

index.js file index.js文件

 var number, origin, destination;

 exports.setNumber = function (num){
    number = num;
 };

 exports.setOrigin = function (o){
    origin = o;
 };

 exports.setDestination = function (d){
    destination = d;
 };

 exports.getInfo = function() {
    return {
        number: number,
        origin: origin,
        destination: destination
    };
 };

app.js file app.js文件

var flight = require('./flight');

flight.setOrgin('LAX');
flight.setDestination('DCA');
flight.setNumber(462);

console.log(flight.getInfo());

Any help with be great. 任何帮助都很棒。 I thought the name within the package.json file was your module but maybe I am incorrect. 我以为package.json文件中的名称是您的模块,但也许我不正确。 Thanks in advance. 提前致谢。

"./flight" does not evaluate to index.js in your case. 在您的情况下,“ ./ flight”不会评估为index.js。

If you create a subdir : 如果创建子目录:

flight
  - index.js

then you can require("./flight") and have it evaluated to "./flight/index.js". 那么您可以require(“ ./ flight”)并将其评估为“ ./flight/index.js”。

In your case, simplest thing you could to do : 就您而言,您可以做的最简单的事情是:

require("./index.js");

you can do it in the following way: 您可以通过以下方式进行操作:

var flight = require('./index.js');

OR 要么

var flight = require('./index');

Following code will not work because the node will look for module named 'flight' in node_modules folder 以下代码将不起作用,因为节点将在node_modules文件夹中查找名为“ flight”的模块

var flight = require('flight');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM