Open mikemaccana opened 1 year ago
I even lean towards saying that when running a ts file ts-node should automatically apply the correct settings (type: module in package.json, module: esnext in tsconfig.ts) if not already properly given.
Hello!
By default, tsc --init
generates config for CJS. CJS code working normal out-of-the-box.
If you need to change between CJS/ESM - try:
"type": "module"
in package.json
"module": "ES2020"
in tsconfig.json
ts-node-esm
instead of ts-node
(or "ts-node": { "esm": true }
in tsconfig.json
)"type": "module"
from package.json
"module": "commonjs"
in tsconfig.json
ts-node
instead of ts-node-esm
(or REMOVE "ts-node": { "esm": true }
in tsconfig.json
)Thanks @SeryiBaran but nobody is asking for instructions on how to use ESM. This is a feature request to use sensible defaults automatically to run TS files (including using ESM).
I wholeheartedly agree. Why do I need to set up a tsconfig.json
file every time I just want to run a completely normal TypeScript script?
This project is way too awesome to have such a learning curve.
By normal, in the year 2023, I mean:
For example: I have a Next.js project that has a database folder which holds migrations. To execute a script to run the migrations, I expect just to do
npx ts-node ./src/backend/db/migrate/index.ts
and it should run.
The hoops you have to jump through if you haven't yet used ts-node:
The import
syntax error
import { migrate } from 'drizzle-orm/planetscale-serverless/migrator';
^^^^^^
SyntaxError: Cannot use import statement outside a module
"type": "module"
to your package.json. TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts"
--esm
as an option.CustomError: Cannot find module '/pathtorepo/src/backend/db/db'
because you imported the db.ts
file like import { db } from '../db';
. from '../db.ts'
- it doesn't work. TS says you need to use .js
.-r dotenv/config
to the command."type": "module"
to your package.json
"ts-node": {
"moduleTypes": {
"./src/backend/db": "esm",
}
},
This all, to run a simple TypeScript script.
Ah, and you still have the file extension .js
in your file import ... from '../db.js';
Hi @michaelschufi, I would like to ask for your permission to print your awesome comment, so I can put it on the wall? Potentially I will print a t-shirt too.
Thanks.
@sangdth Of course, as long as it's for personal use, no problem :)
Desired Behavior
There's a whole bunch of closed tickets, online discussion, workarounds, etc. for this:
Regardless of the contents of package.json, the
ERR_UNKNOWN_FILE_EXTENSION
is not a logical error for a program whose only purpose is to run TypeScript files.Thanks for understanding the frustration of the many people that have tried to use ts-node to run a
.ts
file and been told this isn't possible.Is this request related to a problem?
Additional context
Other tools like
esrun
have been created precisely because of this issue. Rather than close every ticket with 'user error' it would be better if ts-node ran ts files, just like python runs python files and ruby runs ruby files and bash runs bash files.