There are several ways to run JavaScript from the command line:

Node.js: Node.js is a JavaScript runtime that allows you to run JavaScript code on the server-side. You can run JavaScript code using Node.js by creating a JavaScript file and running it with the ‘node’ command. For example, if you have a file named ‘script.js’, you can run it with the following command:

node script.js

npx: npx is a package runner tool that comes with Node.js. It allows you to run JavaScript scripts that are installed as npm packages without the need to install them globally. For example, you can run the ‘create-react-app’ package with the following command:

npx create-react-app my-app

JavaScript shell: Some JavaScript shells like ‘js’ from the Mozilla’s SpiderMonkey engine, ‘jjs’ from Oracle’s Nashorn engine, ‘osascript’ for MacOS allow you to run JavaScript code directly in the shell.

For example, on MacOS you can run the following command:

osascript -e 'console.log("Hello World!")'

Note that the above command will only work on MacOS and you may need to install the JavaScript engine on other operating systems.

It’s also worth noting that you can use JavaScript in some task runners like Grunt, gulp, and webpack to automate repetitive tasks in your development workflow.

Also Read:

Categorized in: