title: NextJS: Run a Typescript script on the server
body: <p>I have a NextJS/Typescript project where I want to add a CLI script which should process some files on the server.</p>
  <p>Unfortunately I don't manage to run the script.</p>
  <p>Example script <code>src/cli.ts</code>:</p>
  <pre><code>console.log(&quot;Hello world&quot;);
  // Process files
  </code></pre>
  <p>I tried to run the script with:</p>
  <pre><code>ts-node src/cli.ts
  </code></pre>
  <p>But I get this error message:</p>
  <pre><code>src/cli.ts:1:1 - error TS1208: 'cli.ts' cannot be compiled under '--isolatedModules' because it is considered a global script file. Add an import, export, or an empty 'export {}' statement to make it a module.
  </code></pre>
  <p>When I add an empty 'export {}' statement I get this error message:</p>
  <pre><code>(node:15923) Warning: To load an ES module, set &quot;type&quot;: &quot;module&quot; in the package.json or use the .mjs extension.
  </code></pre>
  <p>As far as I know it is now not possible to use NextJS with ES modules.</p>
  <p>Is there another way to run the script in a NextJS project? Maybe I need to change the webpack config?</p>
  <p>I'm using the latest versions: Next 11, Typescript 4.3, Node 14.18, ts-node 10.13 with the default <code>tsconfig.json</code>, <code>package.json</code>.</p>