get any module path in a npm monorepo

Viewed 31

I have an NPM monorepo project (ESM):

<root>
|_package.json
|_node_modules/
| |_luxon (1.28.0)
|_packages/
  |_pack1
  | |_node_modules/
  | | |_luxon (3.0.1)
  | |_main.js
  |_pack2
    |_node_modules/
    |_main.js

Now, I need a way to get a module path from any of my main.js files:

For instance, given that luxon has 2 installed versions (in root, pack1), when I execute main.js from pack1, this what I'd like to get:

const luxonPath = (???)("luxon");
// luxonPath is <root>/packages/pack1/node_modules/luxon

From pack2:

const luxonPath = (???)("luxon"); 
// luxonPath is <root>/node_modules/luxon
1 Answers

I think you should use require.resolve()

var luxonPath = require.resolve('luxon');

console.log("luxonPath is" + luxonPath);
Related