#help
1 messages · Page 1 of 1 (latest)
@wide sandal You shouldn't need type: "module" to use import statements in TS. That switches your project over to native ESM, but you shouldn't need to do that. It can be simpler to have TS output CommonJS syntax instead via module: "CommonJS" in the tsconfig
why simpler?
I believe native ESM in node is still a little new and experimental - unless it's gotten a ton better in the last few months, I think it's a bit of a headache to get it set up and working right. Whereas "TS compiles to CJS" has been the standard approach for years.
The only issue you're likely to hit with compile-to-CJS is 1) if you need a very specific feature like top-level await or 2) a few libraries have intentionally dropped support for it in their latest versions.
And more libraries will
There shouldn't be issuses with native ESM, if you want to compile to ESM only then do it
At least I idk about issuses with ESM
People having issues with native ESM has been a pretty common theme here over the last year or two.
What kind of issuses?
Tooling issues, library support issues, configuration issues.
Anyways, this is kind of a tangent here. If you want to recommend people use native ESM, sure. But let's not tell people that they need type: "module" to write import in Typescript.
Neither let's tell them they need to compile to CJS
I didn't say they need to.
I said "can be simpler" and I still think that's generally true. Maybe that'll change over time.
How making TS output CJS could be simpler? And simpler in terms of what?
then can I run ts file as
node src/index.ts
@wide sandal No, you can't do that either way - you need to compile the TS code to JS before you run it.
but when ever I try ts-node src/index.ts it says command not found
You generally don't install a package like ts-node globally, the executable is installed into node_modules/.bin.
You should be able to run it with npm exec ts-node or you can make a package.json script (where binaries are loaded automatically)
It was already there before I kept type: module
So it would give me import error if I remove type module rn
What was already there?
@simple sundial when I do npm exec ts-node Error: Cannot find module './ts-node'
Module commojs was already present in tsconfig
If you're trying to run TS files directly without compiling them, it won't work even with type: "module", as soon as you add a type annotation, it'll fail.
How can I compile them
ts-node or tsc or something like it. (I like ts-node-dev)
I'm not sure why npm exec isn't working.
Basically npm exec thinks ts-node is a file on the all files tab
Did you locally install ts-node?
What u mean by locally install
I did npm I ts-node
Yeah, that should work. So is there a ts-node file inside node_modules/.bin?
You could also try npx instead of npm exec - maybe your npm version is older than npm exec?
Yes
ERR! could not determine executable to run
Are you running this command in the root of your project?
It's a startup command of my pterodactyl panel
I don't know what that is.
Think just its a vps
A third option is to just put ts-node in your start script:
"scripts": {
"start": "ts-node app.ts"
}
Oke
:/home/container$ ts-node src/index.ts
/entrypoint.sh: line 16: ts-node: command not found
@simple sundial
Okay, something is strange with your environment, all of these things should work.
Not sure what to say. These things definitely work in a properly setup environment.
No solution for me?
I've given three that should work; I can't really debug your environment.
Is it possible u make an account on https://game.techstar.live and I will add u to my server
Sorry, not really interested in creating accounts and actually trying to debug in a system I don't know anything about.
Atleast by doing you could help me looking out what error is
At worst, if node_modules/.bin/ts-node exists you should be able to run that directly form the .bin folder.
It does exist
Do I do npm exec node_modules/.bin/ts-node @simple sundial
No, if npm exec isn't working for some reason, you'd just run the executable directly
How can I run directly.
node_modules/.bin/ts-node app.ts
Is that a startup cmd?
It's a unix shell command.
I don't have shell sadly
Well then I really don't know anything about the environment you're trying to run this in.
I guess you should look into seeing how to run npm-installed programs as pterodactyl startup commands
@simple sundial worked but shows cannot compile typescript
@simple sundial everything done just 1 help
src/events/ticketCreate.ts:525:14 - error TS2304: Cannot find name 'interaction'.
Why is interaction not named
Hard to say without seeing the relevant code.
I'm unsure why it's undefined
Where is interaction declared?
Above of it was defined
Can you show that code?
export default new Event(
"interactionCreate",
async (interaction: SelectMenuInteraction) => {
//Return if it's not a button
if (!interaction.isSelectMenu()) {
return;
}
//Return if it does not match ticket customIDs
if (!client.ticketNames.includes(interaction.values[0])) {
return;
}
Whole file?
Maybe you can reproduce the issue in the typescript playground? https://www.typescriptlang.org/play
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
I don't really want to see an entire 500-line file, are you sure the line erroring is still inside the function that has the interaction argument?
@simple sundial you can close this all done