Branka
Branka

Reputation: 479

How To Require Module of a Node Submodule

Lets say the module X has a Y submodule. From my node app that has a dependency on X, how can I require submodule Y?

var Y = require('X:Y'); results in Cannot find module 'X:Y'

Upvotes: 10

Views: 14357

Answers (2)

Peter
Peter

Reputation: 1720

Submodule meaning an export from within the X module?

Try...

require('X/path-to-Y')

Upvotes: 19

alex
alex

Reputation: 12275

It's better to just declare Y as your own dependency. But if you really want to do that, here is how it's done:

// make sure that module X is loaded into a cache
require('X')

// get this module from cache
var module_X = require.cache[require.resolve('X')]

// require submodule Y
var Y = module_X.require('Y')

Upvotes: 11

Related Questions