I have an app I created with create-react-app, using the following versions:
v16.15.1v4.3.2v4.0.3 (though I am open to answers that require react-scripts v5)I want to compile and run an executable NodeJS script written in Typescript using the same configuration as the React App. Additionally, I need to be able to import files from the rest of the app into my script.
e.g. /src/scripts/myscript.ts
import foo from '../util/foo'
const result = foo(process.argv[2])
console.log(result)
The closest I have gotten is:
npx ts-node --esm src/scripts/myscript.ts
However it only works if I add "type": "module" to my package.json, which breaks npm start. Even then, my script breaks on the export keyword.
I can create a package.json in the scripts folder with "type": "module" and a tsconfig that extends the app config:
{
"extends": "../tsconfig.json",
"include": ["**/*.ts"],
"compilerOptions": {
"noEmit": false,
"outDir": "build"
}
}
But this runs into issues with import/export in the imported typescript files.
If react-scripts has the correct config to compile and run Typescript files in its start and build scripts, how do I run a custom script?