avatar

ShīnChvën ✨

Effective Accelerationism

Powered by Druid

Create a CLI with Node.js

Node.js provides the ability to execute JavaScript and TypeScript files directly. Furthermore, it can enable Node.js programs to be run as a global command on your system. Here are the step-by-step instructions for both these tasks.

Run the Code File

Node.js can directly run JavaScript and TypeScript files if a shebang (#!) line is included at the top of the code. This unique line informs the system that the file is a script and points to the interpreter for the script.

Executing a JavaScript File

To execute a JavaScript file, you should begin the file with the shebang line #!/usr/bin/env node as follows:

#!/usr/bin/env node
// ... the rest of your code

This line informs the system that the script should be run with the node interpreter.

Once you've added the shebang line, you can run your JavaScript file directly from the terminal using:

./node-cli.js

Executing a TypeScript File

For TypeScript files, it's common to first compile the TypeScript code into JavaScript. TypeScript is a superset of JavaScript and requires transpilation into JavaScript before it can be executed. Once your TypeScript code is transpiled into JavaScript, you can add the shebang line just like you would for a JavaScript file.

However, if you prefer to run TypeScript files directly without a separate compilation step, you can use ts-node, a tool that needs to be installed separately. It's designed to execute TypeScript code directly in a Node.js environment. In this case, the shebang line should be #!/usr/bin/env ts-node:

#!/usr/bin/env ts-node
// ... the rest of your code

Note that ts-node isn't a standard system interpreter, so it must be installed separately. Also, ensure your script file has the appropriate execution permissions.

After adding the shebang line, installing ts-node, and ensuring proper file permissions, you can run your TypeScript file directly from the terminal using:

./node-cli.ts

Remember, this might not work on all systems without additional configuration.

Run as a Command

In certain situations, you may want your Node.js program to be accessible globally across your system as a command. To achieve this, you need to add a bin field in your package.json file and then link it to your global node_modules.

Modify package.json

Add the bin field in your package.json file to specify the command name and the file to be run when that command is executed:

{
  "bin": {
    "node-cli": "node-cli.js"
  }
}

In this example, node-cli is the command that you want to use, and node-cli.js is the Node.js file that should be run when you use this command.

Linking the Command Globally

Next, link your program to the global node_modules directory. This step makes your command available system-wide.

To link your command, navigate to your project directory and run:

npm link

This command creates a symbolic link from your project to the global node_modules directory.

After linking, you can run your Node.js program anywhere on your system using the command you specified in the bin field of package.json:

node-cli 

Now your Node.js program is globally accessible as a command on your system! This way, you can execute your program from any location on your system without having to refer to the file's full path.