#TS

45 messages · Page 1 of 1 (latest)

molten hedge
#

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.

main hedgeBOT
#
  • 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
daring sage
#

TS normally doesn't shove everything into a single file

molten hedge
daring sage
#

you should use typescript compiler instead

#

"build": "tsup src/index.ts --minify" i think it should be fixed after removing the --minify option

molten hedge
daring sage
#

oh

molten hedge
daring sage
#

just use the typescript compiler npx tsc

molten hedge
daring sage
#

use NodeNext

molten hedge
daring sage
#

it's a little different

molten hedge
#

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

daring sage
#

ah

#

then use ESNext

#

and in moduleresolution select NodeNext

molten hedge
# daring sage 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"
  ]
}```
daring sage
#

huh

#

can you send your TS version

molten hedge
#

sorry for the ping, though have you got any ideas? @daring sage

daring sage
#

nope

native bronze
molten hedge
#

npx tsc index.ts

native bronze
#

That’s the issue…

#

Run npx tsc and it’ll read your tsconfig. If you compile a specific file it won’t read tsconfig

molten hedge
#

Ohhh

molten hedge
native bronze
#

It is, but only in CommonJS. Not in esm, which you probably used as target

molten hedge
native bronze
#

You answered the first part of your question with your second part

molten hedge
#

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?

native bronze
#

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)

molten hedge
#

Ah right

#

I'm not exactly sure why it can't find this file

native bronze
#

file: protocol shouldn’t use \ but only /, that looks like a malformed URI

molten hedge
#

this is what I'm doing
path.join(import.meta.url, '..', '..', '..', 'src', 'temp', 'deviceAuth.json')

#

Oh, I believe it's path.resovel

native bronze
#

Well, import.meta.url is… an URL, not a path. So you need to handle it like an URL

molten hedge
# native bronze Well, import.meta.url is… an URL, not a path. So you need to handle it like an U...

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