Repository navigation
ts-node does not auto-register in child processes because it does not set NODE_OPTIONS #1707
Description
Search Terms
Discord
Sharding
ChildProcess
SubProcecss
Expected Behavior
using ts-node (with nodemon or without it) to test without compiling every time, everything should just work fine without any errors that point that the config is set wrong.
Actual Behavior
when running Discord.js Sharding with ts-node it throughs errors that say that import cant be used outside of a module though inside of the tsconfig.json "moduleResolution" is set to "Node".
Steps to reproduce the problem
- Open a Empty Folder
- install typescript, ts-node if you don't have it allready.
npm i -g typescript ts-node - install discord.js
npm i discord.js - create a tsconfg.json
- put this into the tsconfig
{
"compilerOptions": {
"module": "CommonJS",
"target": "ESNext",
"moduleResolution": "Node",
"esModuleInterop": true,
"outDir": "./build",
}
}- create bot.ts
- put this simple code that just logs in into the file
import { Client, Intents } from 'discord.js';
const bot = new Client({
intents: [
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MEMBERS,
Intents.FLAGS.GUILD_MESSAGES,
Intents.FLAGS.DIRECT_MESSAGES
],
});
bot.on('ready', () => {
console.log('This bot is logged in')
});
bot.login('TOKEN')- run
ts-node bot.tsthis just works fine - create a sharded.ts
- put this simple code inside of it
import discord, { Shard } from 'discord.js';
const manager = new discord.ShardingManager('./bot.ts', {
token: 'TOKEN',
});
manager.on('shardCreate', (shard: Shard) => {
console.log(`Launched shard ${shard.id}`);
})
manager.spawn();- run
ts-node sharded.tsthis will now result with throwing errors that your not allowed to useimportinside of the bot.ts - run tsc
- replace the path inside of the line
const manager = new discord.ShardingManager('./bot.ts', {inside of the compiled sharded.js with './build/bot.js' - run
node build/sharded.jsit works without problems
after asking that problem on the official discord.js Discord Server and Github The answer I got was that this has nothing to do with discord.js and instead is a problem with ts-node in some way I also looked into the sourcedoe of discordjs but they are just using node:child_process and node:worker_threads package to achieve sharding. Other Libraries and Frameworks may also be affected by this.
minimal reproduction
Specifications
- ts-node version: v10.4.0 and latest for minimal reproduction
- node version: v16.14.2
- TypeScript version: v4.6.3
- tsconfig.json, if you're using one:
{
"compilerOptions": {
"module": "CommonJS",
"target": "ESNext",
"noImplicitAny": true,
"sourceMap": true,
"importHelpers": true,
"forceConsistentCasingInFileNames": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"moduleResolution": "Node",
"esModuleInterop": true,
"removeComments": true,
"outDir": "./build",
"baseUrl": ".",
"paths": {
"@base/*": [
"src/base/*"
],
"@baseTypes/*": [
"src/base/types/*"
],
"@classes/*": [
"src/base/classes/*"
],
"@schema/*": [
"src/base/schema/*"
],
"@interactions/*": [
"src/interactions/*"
],
"@utils/*": [
"src/utils/*"
],
"@assets/*": [
"src/assets/*"
],
}
},
"tsc-alias": {
"replacers": {
"base-url": {
"enabled": false
}
}
},
"include": [
"src/**/*"
],
"exclude": [
"node_modules/**/*"
]
}- Operating system and version: Linux Mint 20.3 x85_64
- Kernel: 5.4.0-107-generic
- Shell: zsh 5.8
Activity
cspotcode commentedon Apr 6, 2022
You'll have to set NODE_OPTIONS in order for ts-node to auto-install into child processes. This is not currently something we do automatically, so it would be a feature request to add this. There's probably an issue for this; I recommend doing a search.
Something like this will probably work for you:
NODE_OPTIONS='--require ts-node/register' node ./bot.ts[-]tsconfig doesn't get parsed through to node:child_process | Discord.js Sharding[/-][+]ts-node does not auto-register in child processes because it does not set `NODE_OPTIONS`[/+]snoweuph commentedon Apr 7, 2022
thanks for the quick anwser.
I have pretty much no experience with node options, but that shouldn't be too difficult to solve with a little research, Not that I know more thanks to you I will try some things out
snoweuph commentedon Apr 7, 2022
Okay it works like a charm with the Node Options, and it also works with nodemon, thanks for your help again
snoweuph commentedon Apr 7, 2022
I gonna report that fix to the dicord.js team, so that this gets more knowledgeable :)
snoweuph commentedon Apr 7, 2022
Actually this node-option adds a new problem, that Types somehow don't get parsed the right way anymore, so You can't use this approach with
"noImplicitAny"
set to true, because it will be missing all types
cspotcode commentedon Apr 7, 2022
snoweuph commentedon Apr 7, 2022
I tried
TS_NODE_PROJECT="./tsconfig.json"and
TS_NODE_FILES="true"both together and alone, but none of them helped, I'm probably doing something wrong right?
snoweuph commentedon Apr 7, 2022
or did you mean that I could just disable type checking fo dev only?
cspotcode commentedon Apr 7, 2022
Disabling typechecking is IMO always the correct choice. We keep typechecking support for people who really want it, but I never use it. Run
tsc --noEmitif you want to typecheck. There's no reason for every single process you spawn to repeat that work. So yes, I recommend turning on our SWC option which will make everything faster.https://typestrong.org/ts-node/docs/swc
When using
--require ts-node/register, ts-node is forced to discover your tsconfig relative to cwd unless you specifyTS_NODE_PROJECT. Also remember that if the child process has a different cwd, and ifTS_NODE_PROJECTis a relative path, then it'll be interpreted relative to the child process's cwd. An easy way to avoid that is to setTS_NODE_PROJECTto an absolute path.However, I suspect that if you add
"swc": trueto your roottsconfig.json, and useNODE_OPTIONS, then that is all that you need. It will be fast and will auto-install into each child process.https://typestrong.org/ts-node/docs/configuration#via-tsconfigjson-recommended