Sunday 30 April 2017

Node.js require module behavior is different between operating systems

I've had a strange issue moving my node.js app from development (localhost osx 10.12.4) to production (Both Ubuntu 16.04 and Amazon Linux AMI 2017.03.0 which is CentOS based I believe). I have a certain file that is giving me issues only on the production/Linux servers, not on localhost osx.

I have a module that is two directories above the file that calls it:

controller.counties.js imports the manager module as following: import Manager from '../../common/objects/Manager'; which gets compiled to ES5 as: const Manager = require('../../common/objects/Manager);

Manager.js:

const Manager = {
  req: {},
  res: {},
  data: [],
  init: function(req,res) {
    const mng = this;
    mng.req = req;
    mng.res = res;
    mng.data = req.body;
    return Promise.resolve(mng);
  },
  handle: function(fn, ...params) {
    return async (manager) => {
        const args = getArgs(params, manager);
        const data = await fn(args);
        const mng = this;

        mng.req = manager.req;
        mng.res = manager.res;
        mng.data = data;
        return Promise.resolve(mng);
    };
  },
  pass: function(fn, ...params) {
    return async (manager) => {
        const args = getArgs(params, manager);
        await fn(args);

        const mng = new Manager();
        mng.req = manager.req;
        mng.res = manager.res;
        mng.data = manager.data;
        return Promise.resolve(mng);
    };
   }
};

function getArgs(params, manager) {
  const args = [];
  params.forEach(function(param) {
    if (param.indexOf('.') > -1) {
        const split = param.split('.');
        const value = getDeepValue(manager, split);
        return args.push(value);
    } else {
        const val = manager[param];
        args.push(val);
    }
  }, this);
 return args;
}

export function getDeepValue(object, array) {
 const prop = array[0];
 const val = object[prop];
 if (array.length === 1) {
    return val;
 }
 const newArray = array;
 newArray.splice(0, 1);
 return getDeepValue(val, newArray);
}

module.exports = Manager;

I keep getting cannot find module ../../common/objects/Manager in the Linux OS's but it works fine on localhost OSX.

Does anyone have any clue as to why I might be having these inconsistencies between OSX and Linux? From what I understand, Linux and OSX are supposed to be nearly identical for Node.js, but does Linux prevent importing/requiring modules from a higher directory?



via gradorade

No comments:

Post a Comment