Page MenuHomePhabricator

Module loading not working because of 'Require stack'
Closed, ResolvedPublic

Description

I discovered this while starting restbase (d08ad0e22fdb58de43a6fba294611139df51ef5c) and specifying pathes from the lib directory. When starting, restbase errors with the following message:

{"name":"restbase","hostname":"d55f77d37a5a","pid":478,"level":60,"err":{"message":"Cannot find module 'lib/content_location_filter.js'\nRequire stack:\n- /restbase/node_modules/hyperswitch/lib/router.js\n- /restbase/node_modules/hyperswitch/lib/exports.js\n- /restbase/node_modules/hyperswitch/lib/server.js\n- /restbase/node_modules/hyperswitch/index.js\n- /restbase/node_modules/service-runner/lib/base_service.js\n- /restbase/node_modules/service-runner/lib/master.js\n- /restbase/node_modules/service-runner/service-runner.js","name":"Error","stack":"Error: Cannot find module 'lib/content_location_filter.js'\nRequire stack:\n- /restbase/node_modules/hyperswitch/lib/router.js\n- /restbase/node_modules/hyperswitch/lib/exports.js\n- /restbase/node_modules/hyperswitch/lib/server.js\n- /restbase/node_modules/hyperswitch/index.js\n- /restbase/node_modules/service-runner/lib/base_service.js\n- /restbase/node_modules/service-runner/lib/master.js\n- /restbase/node_modules/service-runner/service-runner.js\n    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:713:15)\n    at Function.Module._load (internal/modules/cjs/loader.js:618:27)\n    at Module.require (internal/modules/cjs/loader.js:771:19)\n    at require (internal/modules/cjs/helpers.js:68:18)\n    at Router._requireModule (/restbase/node_modules/hyperswitch/lib/router.js:83:20)\n    at Router._loadFilter (/restbase/node_modules/hyperswitch/lib/router.js:138:26)\n    at /restbase/node_modules/hyperswitch/lib/router.js:148:29\n    at Array.map (<anonymous>)\n    at Router._loadRouteFilters (/restbase/node_modules/hyperswitch/lib/router.js:147:40)\n    at Router._handleSwaggerSpec (/restbase/node_modules/hyperswitch/lib/router.js:530:14)\n    at /restbase/node_modules/hyperswitch/lib/router.js:276:29\n    at tryCatcher (/restbase/node_modules/bluebird/js/release/util.js:16:23)\n    at Promise._settlePromiseFromHandler (/restbase/node_modules/bluebird/js/release/promise.js:517:31)\n    at Promise._settlePromise (/restbase/node_modules/bluebird/js/release/promise.js:574:18)\n    at Promise._settlePromise0 (/restbase/node_modules/bluebird/js/release/promise.js:619:10)\n    at Promise._settlePromises (/restbase/node_modules/bluebird/js/release/promise.js:699:18)","code":"MODULE_NOT_FOUND","requireStack":["/restbase/node_modules/hyperswitch/lib/router.js","/restbase/node_modules/hyperswitch/lib/exports.js","/restbase/node_modules/hyperswitch/lib/server.js","/restbase/node_modules/hyperswitch/index.js","/restbase/node_modules/service-runner/lib/base_service.js","/restbase/node_modules/service-runner/lib/master.js","/restbase/node_modules/service-runner/service-runner.js"]},"stack":"Error: Cannot find module 'lib/content_location_filter.js'\nRequire stack:\n- /restbase/node_modules/hyperswitch/lib/router.js\n- /restbase/node_modules/hyperswitch/lib/exports.js\n- /restbase/node_modules/hyperswitch/lib/server.js\n- /restbase/node_modules/hyperswitch/index.js\n- /restbase/node_modules/service-runner/lib/base_service.js\n- /restbase/node_modules/service-runner/lib/master.js\n- /restbase/node_modules/service-runner/service-runner.js\n    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:713:15)\n    at Function.Module._load (internal/modules/cjs/loader.js:618:27)\n    at Module.require (internal/modules/cjs/loader.js:771:19)\n    at require (internal/modules/cjs/helpers.js:68:18)\n    at Router._requireModule (/restbase/node_modules/hyperswitch/lib/router.js:83:20)\n    at Router._loadFilter (/restbase/node_modules/hyperswitch/lib/router.js:138:26)\n    at /restbase/node_modules/hyperswitch/lib/router.js:148:29\n    at Array.map (<anonymous>)\n    at Router._loadRouteFilters (/restbase/node_modules/hyperswitch/lib/router.js:147:40)\n    at Router._handleSwaggerSpec (/restbase/node_modules/hyperswitch/lib/router.js:530:14)\n    at /restbase/node_modules/hyperswitch/lib/router.js:276:29\n    at tryCatcher (/restbase/node_modules/bluebird/js/release/util.js:16:23)\n    at Promise._settlePromiseFromHandler (/restbase/node_modules/bluebird/js/release/promise.js:517:31)\n    at Promise._settlePromise (/restbase/node_modules/bluebird/js/release/promise.js:574:18)\n    at Promise._settlePromise0 (/restbase/node_modules/bluebird/js/release/promise.js:619:10)\n    at Promise._settlePromises (/restbase/node_modules/bluebird/js/release/promise.js:699:18)","levelPath":"fatal/startup","msg":"Message not supplied","time":"2019-08-24T11:33:54.719Z","v":0}

The important one seems to be:

Cannot find module 'lib/content_location_filter.js'
Require stack:
- /restbase/node_modules/hyperswitch/lib/router.js
- /restbase/node_modules/hyperswitch/lib/exports.js
- /restbase/node_modules/hyperswitch/lib/server.js
- /restbase/node_modules/hyperswitch/index.js
- /restbase/node_modules/service-runner/lib/base_service.js
- /restbase/node_modules/service-runner/lib/master.js
- /restbase/node_modules/service-runner/service-runner.js

while investigating this issue, it seems, that hyperswitch is trying to load a module from different pathes (absolute, relative and relative to the app base path). However, the way of getting the info, if the error is related to that the module could not be found, is hardcoded to be exactly "Cannot find module $moduleName"[1], which isn't the case in this error message, as an additional require stack is printed.

It would probably be better to use startsWith instead of a string comparison.

[1] https://github.com/wikimedia/hyperswitch/blob/master/lib/router.js#L85