Search code examples
javascriptnode.jsnpmdiscorddiscord.js

Unable to create folder, invalid argument mkdir


So I am unable to run the following code to create a folder if it doesn't exist, which in my case it doesn't (as I am testing the rest of the code to update from v13 to v14 and introduce slash commands), yet I can't get past this bit.

var dir = `./cha/${"<@" + interaction.member.id + ">"}`;

//Creates a folder for the user (if non existent)
if (!fs.existsSync(dir)) {
    await fs.mkdirSync(dir);
}

What happens afterward is whenever the said command is executed, I get this error:

Error: EINVAL: invalid argument, mkdir './cha/<@145716362650714112>'
    at Object.mkdirSync (node:fs:1388:3)
    at Object.execute (C:\Users\pdste\Desktop\Bamborgor\commands\createchar.js:46:14)
    at Object.execute (C:\Users\pdste\Desktop\Bamborgor\events\interactionCreate.js:16:18)
    at Client.<anonymous> (C:\Users\pdste\Desktop\Bamborgor\bam.js:29:44)
    at Client.emit (node:events:525:35)
    at InteractionCreateAction.handle (C:\Users\pdste\Desktop\Bamborgor\node_modules\discord.js\src\client\actions\InteractionCreate.js:97:12)
    at module.exports [as INTERACTION_CREATE] (C:\Users\pdste\Desktop\Bamborgor\node_modules\discord.js\src\client\websocket\handlers\INTERACTION_CREATE.js:4:36)        
    at WebSocketManager.handlePacket (C:\Users\pdste\Desktop\Bamborgor\node_modules\discord.js\src\client\websocket\WebSocketManager.js:352:31)
    at WebSocketShard.onPacket (C:\Users\pdste\Desktop\Bamborgor\node_modules\discord.js\src\client\websocket\WebSocketShard.js:489:22)
    at WebSocketShard.onMessage (C:\Users\pdste\Desktop\Bamborgor\node_modules\discord.js\src\client\websocket\WebSocketShard.js:328:10) {
  errno: -4071,
  syscall: 'mkdir',
  code: 'EINVAL',
  path: './cha/<@145716362650714112>'
}

Solution

  • In Windows-based environments (as it seems you’re working with here), certain characters are considered “reserved” and can’t be used for naming files/directories.

    Specifically, your use of the less than (<) and greater than (>) symbols are likely causing issues, as they are indeed reserved. From Microsoft’s documentation:

    Use any character in the current code page for a name, including Unicode characters and characters in the extended character set (128–255), except for the following:

    • The following reserved characters:
      • < (less than)
      • > (greater than)
      • : (colon)
      • " (double quote)
      • / (forward slash)
      • \ (backslash)
      • | (vertical bar or pipe)
      • ? (question mark)
      • * (asterisk)