I have right now a project with this directory structure:
components
configurations
contents
contexts
displays
hooks
pages
processors
public
types
utils
In the utils folder, I have a bunch of basic TypeScript utils, which get used (amongst other places) in the components React components. I also use utils and to generate content in the contents folder, which can (should) be run in a basic Node.js process. Similarly, I have a types folder which has some type definitions used in both Next.js frontend and Node.js processes. And I have processors which are data processing scripts, also in TypeScript which should run as Node.js processes outside of Next.js. In another project I had models and tests folders, where the models connected through Knex.js to the database, and I ran those tests using Mocha, against the model (all outside Next.js functionality).
Currently (and for the past few years, on personal projects), I have written the utils/processors/types/etc. in plain JS, not using import x from 'x' but const x = require('x'), and doing otherwise basic Node-land conventions. I have tinkered with babel-node and some other typescript projects like ts-node to try and compile the TypeScript and run it in the plain Node.js process, but I always run into complexities/problems when starting a new project, I can't remember what the last problems I faced were, it's been a while since I've tried with TypeScript using it in a Next.js overall project, but running in a plain Node.js project. I have been out of the loop for a little while on what the latest techniques or best practices are in TypeScript land, and how to get my desired workflow working. The desired workflow is to be able to run the TypeScript modules (with import, etc.), as a Node.js process, and otherwise reuse any of the common/shared code that is also used by the Next.js app.
How can I accomplish that in a straightforward way? What tools do I need to add to my package.json? What about for any of the "config" files? What is the idiomatic "Next.js way" of doing this? I can piece together a new "TypeScript compiling Node.js-module workflow", but I would read several blog posts and try out a few projects before landing on something, and it might not be the recommended approach for dealing with this in the Next.js TypeScript project. So wondering if one could point me in the right direction to skip a few steps.
I am getting this when running a script in ts-node.
import fs from 'fs';
^^^^^^
SyntaxError: Cannot use import statement outside a module
Changing esnext to be "module": "commonjs" allows the script to run locally, but I'm sure this is not the right approach.