NodeJS找不到模块

时间:2017-07-11 20:58:49

标签: javascript node.js module

我有实体目录,存储user.js文件,如下所示:

"use strict";

class User {}

module.exports = User;

和我的index.js:

var User = require("./entities/User")

但我收到了错误

  

错误:无法找到模块' ./ entities / User'
      在Function.Module._resolveFilename(module.js:470:15)
      在Function.Module._load(module.js:418:25)
      在Module.require(module.js:498:17)
      at require(internal / module.js:20:19)
      在Socket.socket.on(/home/etours/capstone-etours/index.js:154:21)
      在emitTwo(events.js:106:13)
      在Socket.emit(events.js:194:7)
      at /home/etours/capstone-etours/node_modules/socket.io/lib/socket.js:503:12
      在_combinedTickCallback(内部/进程/ next_tick.js:73:7)
      at process._tickCallback(internal / process / next_tick.js:104:9)

3 个答案:

答案 0 :(得分:0)

require函数区分大小写,因此您必须完全按照您创建的方式编写路径。

如果您使用U小写,它将起作用:D

答案 1 :(得分:0)

您应该尝试以下代码:

<强> user.js的

var User = function() {};
module.exports = User;

var User = class User {   
};

module.exports.User = User;

<强> index.js

var User = require("./user");
console.log(User);

答案 2 :(得分:0)

您也可以尝试以下代码:

<强> user.js的:

private initializeApp(): void 
  {
    this.platform.ready().then(() => {
      console.log("Before login Deblina");
      /**
       * Read in app configuration, get an oAuthV1 ESO token, register device with REBAR Notification Services
       */
       this.configService.Initialize().subscribe(
        () => this.esoService.getV2Token().subscribe(
          (v2Token) => {

            this.tokendata = BASE64.decode(v2Token);
            alert("Token Deblina decoded: " + BASE64.decode(v2Token));
            console.log("Token Deblina decoded: " + BASE64.decode(v2Token));

            this.concatenatedToken = "'" +this.tokendata+ "'";
            alert(this.concatenatedToken);
            console.log(this.concatenatedToken);

            this.myVar = JSON.parse(this.tokendata);
            alert("Now:" + this.myVar.personnelnbr);
            console.log("Now:" + this.myVar.personnelnbr);

            this.myVar = JSON.parse(this.concatenatedToken);
            alert("Now:" + this.myVar.personnelnbr);
            console.log("Now:" + this.myVar.personnelnbr);

          },

          (error) => console.log(error),
          () => { this.nav.setRoot(HomePage)}
        ),
        (error) => console.log(error)
      );

    });

  } 

<强> index.js:

var User={};
module.exports = User;

var User = class User {    
};

module.exports.User = User;