I'm trying to learn typescript by recoding one of my js bots but completely in ts. However, as ts compiles the code into a single .cjs file in, I run into problems as I use JSON files for storage (device auth storage and other small things). In addition, when I load events, it returns an empty array whilst the code is the exact same just in a different language. I'm wondering if it's possible for me to completely rewrite the bot using TS and run the .ts file directly (like how I run my JS version with node .) in stead of compiling into commonjs. This is my first ever time working with TS.
#TS
45 messages · Page 1 of 1 (latest)
- Consider reading #how-to-get-help to improve your question!
- Explain what exactly your issue is.
- Post the full error stack trace, not just the top part!
- Show your code!
- Issue solved? Press the button!
✅Marked as resolved by OP
TS normally doesn't shove everything into a single file
i used this to get started which uses tsup to compile it into a dist which is probably why. Do you know why I can't simply do node index.ts? It tells me unknown file type .ts
because node.js isn't made to run typescript
you should use typescript compiler instead
"build": "tsup src/index.ts --minify" i think it should be fixed after removing the --minify option
i tried removing --minify, all minify did was compress it to a single line
oh
could you potentially refer me to a guide like the one I linked, just a better one?
just use the typescript compiler npx tsc
do you know what could be causing this?
use NodeNext
I presume that's ESNext?
it's a little different
Value is not accepted. Valid values: "ES3", "ES5", "ES6", "ES2015", "ES2016", "ES2017", "ES2018", "ES2019", "ES2020", "ES2021", "ES2022", "ES2023", "ESNext".(1)
Argument for '--target' option must be: 'es3', 'es5', 'es6', 'es2015', 'es2016', 'es2017', 'es2018', 'es2019', 'es2020', 'es2021', 'es2022', 'esnext'.
Do I have to install NodeNext? @daring sage
ah
then use ESNext
and in moduleresolution select NodeNext
same error
{
"compilerOptions": {
"target": "ESNext",
"module": "NodeNext",
"rootDir": "./",
"removeComments": true,
"resolveJsonModule": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"strictNullChecks": true,
"skipLibCheck": true,
"moduleResolution": "NodeNext"
},
"include": [
"**/*.ts"
]
}```
huh
can you send your TS version
sorry for the ping, though have you got any ideas? @daring sage
nope
What command are you running that causes this?
npx tsc index.ts
That’s the issue…
Run npx tsc and it’ll read your tsconfig. If you compile a specific file it won’t read tsconfig
Ohhh
Do you know why I'm getting ReferenceError: __dirname is not defined? Is this not a global node var
It is, but only in CommonJS. Not in esm, which you probably used as target
Ah. Is there something else I could use for esm or is it import.meta.url?
You answered the first part of your question with your second part
Yeah I wasn't sure though I suppose it's trial and error.
Another thing, I have a folder "temp" with a deviceAuth.json file inside it though it isn't being included in the compiler?
Correct. That folder should (best case) reside outside your src folder and be accessible from src and dist folder the same
Else you‘d need something like tsup or similar to also copy non-ts files over to dist (unchanged)
Ah right
I'm not exactly sure why it can't find this file
file: protocol shouldn’t use \ but only /, that looks like a malformed URI
this is what I'm doing
path.join(import.meta.url, '..', '..', '..', 'src', 'temp', 'deviceAuth.json')
Oh, I believe it's path.resovel
Well, import.meta.url is… an URL, not a path. So you need to handle it like an URL
I forgot about this. Device auth works now but Idk why but eventFiles is returning an empty array when I define my event file path, however when I do glob('**') it returns an array of everything
const eventFiles: string[] = await glob(path.join(fileURLToPath(import.meta.url), '..', '..', 'events', '**', '*.js')) as string[];
console.log(eventFiles)
console.log(path.join(fileURLToPath(import.meta.url), '..', '..', 'events', '**', '*.js'))
// C:\Users\...\Desktop\...ts\dist\events\**\*.js```
It returns the correct file path