ovidiu-miu
ovidiu-miu

Reputation: 57

Is there something similar to the Deno's script installer for the NodeJS runtime?

Deno provides the 'deno install' command which lets you install your scripts, so you can call them directly in your terminal from anywhere.

What it actually does is:

This command creates a thin, executable shell script which invokes deno using the specified CLI flags and main module. It is placed in the installation root's bin directory.

Is there a similar tool for the NodeJS ecosystem?

Upvotes: 0

Views: 360

Answers (2)

justin.m.chase
justin.m.chase

Reputation: 13665

NodeJS has this built-in as well but the feature comes from npm called "bin" scripts which can install commands relatively or globally, when installing a dependency.

The trick is to add a section into your package.json called "bin" and it will then install a very similar thin wrapper script for a command with that name. If you install the dependency globally then it will install a global command, if local then the script can be invoked locally with npx.

So for example here is one I did called hashicon

npm i -g hashicon-cli
hashicon --version
1.0.3

Or if you want it installed relative to this app you can then invoke it with npx:

npm i hashicon-cli
npx hashicon --version

npx will add the relative ./node_modules/.bin folder to the path when invoking the script. Additionally, the relative bin folder is added to the path when invoking npm run scripts, so you can directly reference the commands in your package.json "scripts".

Also, relatedly, I created a scripts script for Deno that you can check out so you can can similarly have relative scoped Deno scripts as well.

Upvotes: 3

mfulton26
mfulton26

Reputation: 31234

With Node.js you can do something similar to deno install manually (at least on Linux/macOS)…

  1. create a script file
  2. add a shebang to tell the OS that this is a Node.js script
  3. mark the file as executable
  4. place the file somewhere on your PATH

Example

greet.js

#!/usr/bin/env node

console.log("Hello World!");
chmod +x ./greet.js
mv greet.js /usr/local/bin/greet

This assumes that /usr/local/bin is already in your PATH; the ".js" extension is dropped so that you don't have to specify it when using it

% greet
Hello World!

You can also create NPM packages that can be installed as CLI tools (e.g. with npm i -g <package-name>). There are various blog posts out there about doing do. Here's one: Creating a CLI tool with Node.js.

Upvotes: 3

Related Questions