How To Require Module of a Node Submodule

后端 未结 2 588
迷失自我
迷失自我 2021-01-12 02:07

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

相关标签:
2条回答
  • 2021-01-12 02:42

    Submodule meaning an export from within the X module?

    Try...

    require('X/path-to-Y')
    
    0 讨论(0)
  • 2021-01-12 02:51

    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')
    
    0 讨论(0)
提交回复
热议问题