Fireship · @fireship

I created a Command Line Game for you // 5-Minute Node.js CLI Project

Uploaded January 27, 2022 · Length 6:25

I created a Command Line Game for you // 5-Minute Node.js CLI Project

Summary

Create a command line game using Node.js, including animation and user input, and deploy it via npm.

  • node.js cli project
  • command line game
  • javascript game
  • cli animation
  • user input in cli
  • npm deployment

Transcript preview

The first 2 minutes of the transcript. Select a timestamp to watch that moment on YouTube.

  1. 0:00

    the one thing that makes developers

  2. 0:01

    different than the common people out

  3. 0:02

    there is the command line the moment

  4. 0:04

    someone opens up a terminal and types

  5. 0:06

    out their first command they're

  6. 0:07

    instantly transformed into a jedi hacker

  7. 0:09

    in today's video we'll take things to

  8. 0:11

    the next level by building a crazy

  9. 0:12

    command line tool from scratch in fact

  10. 0:14

    you can try out the app we're building

  11. 0:15

    right now by running npx fire quiz from

  12. 0:18

    your terminal that's because i deployed

  13. 0:19

    it to npm as an executable and i'll show

  14. 0:21

    you how you can too now i realize that

  15. 0:24

    people will bash me for not using bash

  16. 0:25

    in this video but the node.js ecosystem

  17. 0:28

    has all kinds of awesome tools that can

  18. 0:29

    help you build an over-the-top command

  19. 0:31

    line tool that'll make people think

  20. 0:32

    you're some kind of coding wizard if

  21. 0:34

    you're new here like and subscribe then

  22. 0:35

    the first person to win the game of who

  23. 0:36

    wants to be a javascript millionaire

  24. 0:38

    gets a free fire ship hoodie let's first

  25. 0:40

    take a look at what we're building today

  26. 0:41

    we have a command line tool that

  27. 0:42

    animates the text who wants to be a

  28. 0:44

    javascript millionaire then it will

  29. 0:45

    prompt you to enter your name after that

  30. 0:48

    it goes through a series of multiple

  31. 0:49

    choice questions when you enter an

  32. 0:51

    answer it will show a loading spinner if

  33. 0:52

    you get it right it will continue but if

  34. 0:54

    you get it wrong it will exit the

  35. 0:56

    command line script and call you a loser

  36. 0:58

    if you make it all the way to the end

  37. 0:59

    you win it will show you some ascii text

  38. 1:01

    with a color gradient and a message that

  39. 1:03

    when left in the comments on this

  40. 1:04

    youtube video wins you a free hoodie to

  41. 1:06

    get started with this tutorial create an

  42. 1:08

    empty directory now go ahead and open

  43. 1:09

    that directory in vs code then run npm

  44. 1:12

    init flag y that will create a new

  45. 1:13

    package json file where we can install

  46. 1:15

    node dependencies now i have a list of

  47. 1:17

    packages to install here and i'll

  48. 1:19

    explain what each one does as we get

  49. 1:20

    into the code inside the package json

  50. 1:22

    we'll want to change the type to module

  51. 1:24

    doing that tells node.js that we want to

  52. 1:26

    use esm modules allowing us to use the

  53. 1:28

    import export syntax instead of the

  54. 1:30

    commonjs require function now let's

  55. 1:32

    create an index.js file for the source

  56. 1:34

    code and inside of it the first thing

  57. 1:36

    we'll do is add a shebang at the top

  58. 1:38

    you'll always want to include one of

  59. 1:39

    these when writing a command line script

  60. 1:40

    for somebody else to use it tells the

  61. 1:42

    operating system to execute the code

  62. 1:44

    with the node.js version installed on

  63. 1:46

    the user's local system from there i'm

  64. 1:47

    importing all of our dependencies and

  65. 1:49

    the first one that i want to show you

  66. 1:50

    how to use is chalk which is created by

  67. 1:52

    one of the most prolific open source

  68. 1:54

    creators out there syndrasaurus an

  69. 1:56

    actual codewizard its purpose is very

  70. 1:58

    simple it will color the output text in