Skip to content

ts-node does not auto-register in child processes because it does not set NODE_OPTIONS #1707

Description

@snoweuph

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.ts this 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.ts this will now result with throwing errors that your not allowed to use import inside 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.js it 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

TypeStrong/ts-node-repros#27

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

  1. cspotcode commented on Apr 6, 2022

    @cspotcode
    Collaborator

    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
  2. changed the title [-]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`[/+] on Apr 6, 2022
  3. snoweuph commented on Apr 7, 2022

    @snoweuph
    Author

    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

  4. snoweuph commented on Apr 7, 2022

    @snoweuph
    Author

    Okay it works like a charm with the Node Options, and it also works with nodemon, thanks for your help again

  5. snoweuph commented on Apr 7, 2022

    @snoweuph
    Author

    I gonna report that fix to the dicord.js team, so that this gets more knowledgeable :)

  6. snoweuph commented on Apr 7, 2022

    @snoweuph
    Author

    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

  7. cspotcode commented on Apr 7, 2022

    @cspotcode
    Collaborator
  8. snoweuph commented on Apr 7, 2022

    @snoweuph
    Author

    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?

  9. snoweuph commented on Apr 7, 2022

    @snoweuph
    Author

    or did you mean that I could just disable type checking fo dev only?

  10. cspotcode commented on Apr 7, 2022

    @cspotcode
    Collaborator

    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 --noEmit if 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 specify TS_NODE_PROJECT. Also remember that if the child process has a different cwd, and if TS_NODE_PROJECT is a relative path, then it'll be interpreted relative to the child process's cwd. An easy way to avoid that is to set TS_NODE_PROJECT to an absolute path.

    However, I suspect that if you add "swc": true to your root tsconfig.json, and use NODE_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

  11. locked and limited conversation to collaborators on Apr 7, 2022
  12. converted this issue into a discussion #1708 on Apr 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions