log4js-node/log4js-api

Logs not showing for locally installed library packages

lightpriest opened this issue · 2 comments

When log4js is used in a library which is locally installed (npm install <dir> or npm link <package>) it seems to not work.

We're using @log4js-node/log4js-api in one of our library packages, and when we run it locally its logs are not showing. I have created a package-library which uses log4js-api, and used it in a test project. Stripped down from every other dependency or build tool:

// Project
const log4js = require('log4js');
log4js.configure({
	appenders: { stdout: { type: 'stdout' } },
	categories: { default: { appenders: ['stdout'], level: 'ALL' } }
})

const lib = require('package-library');

const logger = log4js.getLogger('project');
logger.info('Hello');

lib();

log4js.shutdown(() => {});
// package-library/index.js
const log4js = require('@log4js-node/log4js-api');

module.exports = () => {
  const logger = log4js.getLogger('library');
  logger.info('Hello');
}

I'm seeing that it errors when requiring log4js with a MODULE_NOT_FOUND error.

{ Error: Cannot find module 'log4js'
    at Function.Module._resolveFilename (module.js:548:15)
    at Function.Module._load (module.js:475:25)
    at Module.require (module.js:597:17)
    at require (internal/module.js:11:18)
    at checkForLog4js (/.../package-library/node_modules/@log4js-node/log4js-api/lib/index.js:24:12)
    at Object.<anonymous> (/.../package-library/node_modules/@log4js-node/log4js-api/lib/index.js:31:16)
    at Module._compile (module.js:653:30)
    at Object.Module._extensions..js (module.js:664:10)
    at Module.load (module.js:566:32)
    at tryModuleLoad (module.js:506:12) code: 'MODULE_NOT_FOUND' }

When installing it through the "standard" channels (git repo or npm repo) it works as expected. What am I missing? I think it's not log4js' fault, but rather how npm loads locally installed libraries.

I think both npm install <folder> and npm link both use symlinks, so any directory traversal back up the tree in node_modules from the package-library to the main project will not work as expected.

That's what I figured as well, though the log4js require is not hack-ish in any way. Seems like npm should find modules when installed like this. I'll try to re-produce without log4js and see maybe it's an undocumented behavior or a bug.