title: How should I import JSON in Node?
body: <p>I want to import <code>package.json</code> into <code>test.js</code>, both files are in same directory.</p>
  <p>I tried with <code>require</code> :</p>
  <pre><code>const jsonfile = require(&quot;./packages.json&quot;);
  
  console.log({ jsonfile });
  </code></pre>
  <p>it throws error:</p>
  <pre><code>file:///home/.../test.js:1
  const jsonfile = require(&quot;./packages.json&quot;);
                   ^
  ReferenceError: require is not defined
      at file:///home/.../test.js:1:18
      at ModuleJob.run (internal/modules/esm/module_job.js:145:37)
      at async Loader.import (internal/modules/esm/loader.js:182:24)
      at async Object.loadESM (internal/process/esm_loader.js:68:5)
  
  </code></pre>
  <p>This error implies, like it runs in browser, where is no <code>require</code>,  I found <a href="https://stackoverflow.com/a/19059825/196241">an answer</a> with similar message.</p>
  <p>I tried with <code>import</code>:</p>
  <pre><code>import * as jsonfile from './packages.json';
  
  console.log({ jsonfile });
  </code></pre>
  <pre><code>internal/process/esm_loader.js:74
      internalBinding('errors').triggerUncaughtException(
                                ^
  
  Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/home/.../packages.json' imported from /home/.../test.js
      at finalizeResolution (internal/modules/esm/resolve.js:271:11)
      at moduleResolve (internal/modules/esm/resolve.js:694:10)
      at Loader.defaultResolve [as _resolve] (internal/modules/esm/resolve.js:784:11)
      at Loader.resolve (internal/modules/esm/loader.js:100:40)
      at Loader.getModuleJob (internal/modules/esm/loader.js:246:28)
      at ModuleWrap.&lt;anonymous&gt; (internal/modules/esm/module_job.js:47:40)
      at link (internal/modules/esm/module_job.js:46:36) {
    code: 'ERR_MODULE_NOT_FOUND'
  }
  </code></pre>
  <p>What I have tried more?</p>
  <ul>
  <li>Single quotes and double quote around filename</li>
  <li>filename with extension and without.</li>
  <li>with flag <a href="https://stackoverflow.com/a/60206393/196241">--experimental-json-modules</a></li>
  <li><a href="https://stackoverflow.com/a/45854500/196241">adding <code>&quot;type&quot;:&quot;module</code></a> into <code>package.json</code> (but this is for Node &gt;= 13, but without it I got warning <code>(node:7170) Warning: To load an ES module, set &quot;type&quot;: &quot;module&quot; in the package.json or use the .mjs extension.</code> and then also error <code>SyntaxError: Cannot use import statement outside a module</code>)</li>
  </ul>
  <p>I found even suggestions to load JSON-file as regular text files and parse them into data structure, but this seems like the last resort...</p>
  <p>It seems such a trivial task, but I have not found the idiomatic way how to import JSON-data into Javascript variable. What is wrong here? How should I import it?</p>
  <p>I use Node 12.21.0</p>