• Jobs
  • About Us
  • Jobs
    • Home
    • Jobs
    • Courses and challenges
  • Businesses
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

130
Views
How do I run a NodeJS script in typescript with create-react-app?

I have an app I created with create-react-app, using the following versions:

  • Node v16.15.1
  • Typescript v4.3.2
  • react-scripts v4.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?

over 3 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Show me some job opportunities
There's an error!