假设模块X有一个Y子模块.从我的依赖于X的节点应用程序,我如何需要子模块Y?
var Y = require('X:Y'); 结果是 Cannot find module 'X:Y'
最好只将Y声明为自己的依赖项.但如果你真的想这样做,这就是它的完成方式:
// 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')
Run Code Online (Sandbox Code Playgroud)