title: find npm package folders
body: <p>I am trying to get a list of all dependencies of a given npm package.</p>
  <p>Given the following hierarchy:</p>
  <pre><code> 1. ./node_modules/@dependency-group/dependency1/package.json
   2. ./node_modules/dependency2/package.json
   3. ./node_modules/dependency2/node_modules/dependency3/package.json
  </code></pre>
  <p>Desired result:</p>
  <pre><code>@dependency-group/dependency1
  dependency2
  dependency3
  </code></pre>
  <p>I wrote the following script (Should be executed in project root dir):</p>
  <pre><code>find -iregex '\.\/(node_modules\/((@[^\/]+\/)?[^\/]+)\/)+package\.json' -printf '%h\n' | awk -F'/' '{tmp = $(NF-1)&quot;/&quot;$(NF); module = gsub(&quot;node_modules/&quot;,&quot;&quot;,tmp); print tmp}' | grep -v &quot;*&quot;
  </code></pre>
  <p>Explanation: find any of variations, get path without 'package.json' node, get last and one before last paths. if the one before last node is 'node_modules', remove it, else if it's a package group keep it.</p>
  <p>But There is a problem in the regex.</p>
  <p>If I change it to a more basic one it partially works:</p>
  <pre><code>find -iregex '\.\/node_modules\/[^\/]+\/package\.json' -printf '%h\n' | awk -F'/' '{tmp = $(NF-1)&quot;/&quot;$(NF); module = gsub(&quot;node_modules/&quot;,&quot;&quot;,tmp); print tmp}' | grep -v &quot;*&quot;
  </code></pre>
  <p>result: <code>dependency2</code></p>
  <p>in JS the regex works fine:</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>const regex = /\.\/(node_modules\/((@[^\/]+\/)?[^\/]+)\/)+package\.json/gi;
  const str = `./node_modules/@dependency-group/dependency1/package.json
  ./node_modules/dependency2/package.json
  ./node_modules/dependency2/node_modules/dependency3/package.json`;
  let m;
  
  while ((m = regex.exec(str)) !== null) {
      console.log(m[0]);
  }</code></pre>
  </div>
  </div>
  </p>
  <p>How to make the regex fit find command?</p>