ts-node vs tsc
when i compile with tsc the code works fine, but with ts-code it stops for some reasons
28 Replies
with tsc and node dist/index it works fine
but with ts-node it stops like this
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
.and read the big red block in the guide: https://www.sapphirejs.dev/docs/Guide/getting-started/getting-started-with-sapphire
Sapphire Framework
Getting started with Sapphire | Sapphire
To install Sapphire, you need to install both discord.js and
ah i see. i was using tsc but then i though that ts-node is better
besides do you know why this happen
im using module-alias/register
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-L16I don't actually see you mention about tsc / ts-node in any of those red blocks unless I'm being blind
no that's about validating the main property
Ohhhh okay
because with ts-node it would be
src/main.ts
but with tsc
it has to be dist/main.js
(or similar)yes in the index file
move it up 2 lines
it has to be the very first line
Code is always executed from top to bottom
ok
still give the same thing
ill try this
didn't work :-
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
Ye that's what i dis
Thanks for helping
yw
Can i ask you what you think is better npm, pnpm or yarn
@Favna
Never even heard of pnpm, but I just stay basic and use NPM personally
Even though arguably, yarn is faster
yarn - specifically v3
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: 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:
This will add to your .yarnrc
file:
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
:
And anywhere in your scripts in package.json where you use *
you should wrap it in extra "
For example:
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:
So yarn is fastest, i see
Nice i'll switch to yarn
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.