I am working on a NextJS/Typescript project where I need to implement a CLI script for processing files on the server.
However, I am facing difficulties in getting the script to run successfully.
Here is an example of the script src/cli.ts
:
console.log("Hello world");
// Process files
I attempted to execute the script using the following command:
ts-node src/cli.ts
But I encountered this error message:
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.
Upon adding an empty 'export {}' statement, I received the following warning:
(node:15923) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
It appears that using ES modules with NextJS is currently not supported.
Is there an alternative approach to running the script within a NextJS project? Perhaps modifying the webpack configuration could help?
My setup includes the latest versions: Next 11, Typescript 4.3, Node 14.18, ts-node 10.13 with the default tsconfig.json
, package.json
.