简体   繁体   中英

How do you import a “subdependency” in ES6?

I have a package react-router , which depends on path-to-regexp . react-router does not export its own import of path-to-regexp . In my top-level module, how do I import the exact same path-to-regexp that react-router uses? Or is this not possible?

For reference, I'm using Yarn as my dependency manager, and I'm considering using its resolution configuration for this purpose, but I think version mismatches could become an issue down the line.

If I could import path from 'react-router/node_modules/path-to-regexp' , I would, but that doesn't seem to work. If I list path-to-regexp as a dependency of my top level module, I could still get version mismatches down the line. Maybe there's a way to use node-semver globs to reference the same version as react-router in package.json ?

I believe using the resolutions feature of yarn is the best option. I can't reuse exactly the same module, but I can have a duplicate of exactly the same version in my module. And if there is a version mismatch, yarn will let me know with a warning, as described in this RFC . In other words, my package.json looks like this...

"dependencies": {
  "react-router": "^4.2.0",
  "path-to-regexp": "^1.7.0" <-- same version used by react-router@4.2.0
},
"resolutions": {
  "path-to-regexp": "^1.7.0"
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM