ts-node vs tsc

when i compile with tsc the code works fine, but with ts-code it stops for some reasons
28 Replies
Youssef
YoussefOP17mo ago
with tsc and node dist/index it works fine
Youssef
YoussefOP17mo ago
No description
Youssef
YoussefOP17mo ago
but with ts-node it stops like this
No description
Spinel
Spinel17mo ago
TL;DR: Do not use ts-node, use tsc-watch instead. We very strongly discourage using ts-node because it was never meant to be used for bots. ts-node is designed for REPL purposes. That's short for Read Eval Print Loop. Which means to read some code, dump it in an eval() statement, print the result, and loop. A discord bot is not that. A Discord bot sets up a permanent websocket connection to the discord server and connects to the rest gateway. There is read yes, but no eval, no print, and no loop. So what should you use instead? The most ideal way is to just use the watch flag of tsc (tsc --watch) and run node dist/index.js to run your bot, then cancel that process and restart it when you have changes that require restarting. You would open 2 terminal tabs, 1 in which you run tsc --watch and another in which you run the bot. This is in particular the most ideal way, because Discord has a limit to the amount of times you can login with your bot, or register commands, per day. Constantly logging in over and over again due to an auto-restarting process will get you close to that limit very quickly and once you exceed it, your development will be halted entirely for the current day. However, this can be quite tedious so a great package to use instead is tsc-watch.
Favna
Favna17mo ago
Sapphire Framework
Getting started with Sapphire | Sapphire
To install Sapphire, you need to install both discord.js and
Youssef
YoussefOP17mo ago
ah i see. i was using tsc but then i though that ts-node is better besides do you know why this happen
Youssef
YoussefOP17mo ago
No description
Youssef
YoussefOP17mo ago
No description
No description
Youssef
YoussefOP17mo ago
im using module-alias/register
Favna
Favna17mo ago
are you actually loading module-alias preferebly as the first line of the code, with import module-alias/register? Also NodeJS has native support for path aliases, you just have to use # instead of @. Example bot using it tsconfig: https://github.com/favware/dragonite/blob/main/src/tsconfig.json package.json: https://github.com/favware/dragonite/blob/5e6b386632d9840b9f254ce9bfbc9a1a4899b402/package.json#L10-L16
b1nzee
b1nzee17mo ago
I don't actually see you mention about tsc / ts-node in any of those red blocks unless I'm being blind
Favna
Favna17mo ago
no that's about validating the main property
b1nzee
b1nzee17mo ago
Ohhhh okay
Favna
Favna17mo ago
because with ts-node it would be src/main.ts but with tsc it has to be dist/main.js (or similar)
Youssef
YoussefOP17mo ago
yes in the index file
No description
Favna
Favna17mo ago
move it up 2 lines it has to be the very first line Code is always executed from top to bottom
Youssef
YoussefOP17mo ago
ok still give the same thing
Youssef
YoussefOP17mo ago
ill try this
No description
Youssef
YoussefOP17mo ago
didn't work :-
Favna
Favna17mo ago
I'd say just try the node native stuff I talked about It's much faster to load modules that way too And you'll be more set for a full ESM bot
Youssef
YoussefOP17mo ago
Ye that's what i dis Thanks for helping
Favna
Favna17mo ago
yw
Youssef
YoussefOP17mo ago
Can i ask you what you think is better npm, pnpm or yarn @Favna
b1nzee
b1nzee17mo ago
Never even heard of pnpm, but I just stay basic and use NPM personally Even though arguably, yarn is faster
Favna
Favna17mo ago
yarn - specifically v3
Spinel
Spinel17mo ago
Yarn v3 is new version of Yarn that we recommend switching to as Yarn v1 has been deprecated.
"But I don't see any update on [source]?"
That is correct. Yarn v3 is installed through Yarn itself. Your global toolchain is and will always remain to be Yarn v1, however, you configure Yarn v3 on a per-project basis. How? Simply write:
yarn set version berry
yarn set version berry
This will download the new Yarn v3 binary and put in .yarn/releases, you should push this to your Git repository. It will also create a .yarnrc.yml file which configures the path which you should also commit. Next you probably also want to run the following 2 commands:
yarn config set enableGlobalCache true
yarn config set nodeLinker node-modules
yarn config set enableGlobalCache true
yarn config set nodeLinker node-modules
This will add to your .yarnrc file:
enableGlobalCache: true
nodeLinker: node-modules
enableGlobalCache: true
nodeLinker: node-modules
Which ensures that you just have a Yarn v1-like experience with node_modules and a global cache. Next step is to nuke your node_modules and yarn.lock and run yarn install Then some final adjustments. Put this in you .gitignore:
# Yarn files
.yarn/install-state.gz
.yarn/build-state.yml
# Yarn files
.yarn/install-state.gz
.yarn/build-state.yml
And anywhere in your scripts in package.json where you use * you should wrap it in extra " For example:
{
"format": "prettier --write \"src/**/*.ts\""
}
{
"format": "prettier --write \"src/**/*.ts\""
}
Mind you this last thing is good add regardless of script runner / package bundler because it ensures the glob is performed by the library and not by your shell, which may differ when people develop on different operating systems. In short the command to set everything up you can run:
yarn set version berry && yarn config set enableGlobalCache true && yarn config set nodeLinker node-modules && echo "" >> .gitignore && echo "# Yarn files" >> .gitignore && echo ".yarn/install-state.gz" >> .gitignore && echo ".yarn/build-state.yml" >> .gitignore
yarn set version berry && yarn config set enableGlobalCache true && yarn config set nodeLinker node-modules && echo "" >> .gitignore && echo "# Yarn files" >> .gitignore && echo ".yarn/install-state.gz" >> .gitignore && echo ".yarn/build-state.yml" >> .gitignore
Youssef
YoussefOP17mo ago
So yarn is fastest, i see Nice i'll switch to yarn
Favna
Favna17mo ago
put it this way. Try to search a package, any package, on npmjs.org, then do the same on yarnpkg.com. Then tell me who you think cares more about speed and maintaining that speed.

Did you find this page helpful?