Nodejs错误:在ec2中找不到模块..

时间:2016-03-22 01:08:39

标签: node.js hapijs aws-ec2

我的项目树如下所示:

models/userStore.js

index.js

我使用以下命令在index.js中导出用户信息。 const UserStore = require('./models/UserStore');

它在我的本地机器上工作正常。当我在ec2上传时,我收到了以下错误。

module.js:341
    throw err;
    ^

Error: Cannot find module '../models/UserStore'
    at Function.Module._resolveFilename (module.js:339:15)
    at Function.Module._load (module.js:290:25)
    at Module.require (module.js:367:17)
    at require (internal/module.js:16:19)
    at Object.<anonymous> (/home/ec2-user/test/dist/index.js:13:17)
    at Module._compile (module.js:413:34)
    at Object.Module._extensions..js (module.js:422:10)
    at Module.load (module.js:357:32)
    at Function.Module._load (module.js:314:12)
    at Function.Module.runMain (module.js:447:10)

index.js

 'use strict';

const Hapi = require('hapi');
const HapiSwagger = require('hapi-swagger');
const server = new Hapi.Server();
const Joi = require('joi');
const jwt = require('jsonwebtoken');
const db = require('./db');
const UserStore = require('./models/UserStore');
UserStore.initialize();
const user = UserStore.users; 

server.connection({port: 3000});
server.register(require('./plugins/plugins.js'));

server.register([
    {
        'register': HapiSwagger    
    }
    ], (err) => {
        server.start( (err) => {
           if (err) {
                console.log(err);
            } else {
                console.log('Server running at:' + server.info.uri);
            }
        });
    });


var validate = function (request, decodedToken, callback) {
    var user = UserStore.users[decodedToken.email];
    if (!user) {
      return callback(null, false);
    }
    return callback(null, true, user);
};

server.register(require('hapi-auth-jwt'), function () {
  server.auth.strategy('token', 'jwt', { key: privateKey,
                                         validateFunc: validate,
                                         verifyOptions: { algorithms: [ 'HS256' ] }
                                       });
    server.route(require('./routes/endpoints/routes.js'));
});

1 个答案:

答案 0 :(得分:0)

我解决了这个问题。问题是我的文件名是(userStore),当我导出我使用(UserStore)。由于mac不区分大小写,因此它在本地运行良好。由于linux文件系统区分大小写,因此会抛出该错误。

更多详情:Can I force node.js require to be case sensitive?