1

I made a javascript file mytool.js, it has some dependencies (in package.json).

Now I can execute typing in

~/mynodes/mytool $ node mytool

But if I change the working directory I can't use this command anymore because previously it was run locally. What I want to achieve is to be able to just type :

~$ mytool

(wherever I am in my system's filesystem and without typing node before).

Should I install it manually ?

If yes, where is the common location to install a personal nodejs script in a unix-like system ?

Or is there a npm-like command to install a personal script system wide ?

3 Answers 3

2

When you add a "bin" key in your package.json:

  "bin": {
    "mytool": "mytool.js"
  },

then you will be able to install your script with npm install -g and it will be automatically added where it should be (to a place where other globally installed cli tools are installed, which should be in your PATH).

You can see this simple project as an example:

It was created as an example for this answer but it does what you need:

  1. it has a single script to run
  2. it has external dependencies
  3. it can be installed globally
  4. it can be run from any place with a single command

Note that you don't need to publish your script to npm to be able to install it - though you can do it, or you can also install projects directly from GitHub (including private repos) - but you can also install a module that you have in your local directory or a tarball:

npm install -g module-on-npm
npm install -g user/repo-on-github
npm install -g /your/local/directory
npm install -g /your/local/tarball.tgz

For more options, see:

Also keep in mind that for your program to be able to be executed from anywhere, you need to use paths relative to __dirname or __filename if you need to access your own files relative to your code. See:

Sign up to request clarification or add additional context in comments.

Comments

1
  1. Put a shebang line at the top of the script (e.g. #!/usr/bin/env node).
  2. Put the script in a directory in your $PATH
  3. Give it executable permission (e.g. chmod +x /usr/local/bin/example.js)

1 Comment

I like the idea so I upvoted but the problem is I have to manually install the dependencies the same way which is not really convenient.
1

First option:

You can run your file globally by putting on the first line of the file : #!/usr/bin/env node, copying it to /usr/local/bin and make it executable: sudo chmod +x /usr/local/bin/yourfile.js and then you can call it from where you want with yourfile.js

Second option:

Make your local file executable and create an executable bash script which calls your local file and put it in /usr/local/bin and then call the bashfile globally.

5 Comments

This will work but I would recommend putting your own files to /usr/local/bin if you need to instead of /usr/bin which is usually managed by the package manager of the OS.
@rsp I think /usr/bin is fine, I tried what you suggested and npm install -g will install the package in that directory.
If you have dependencies in the local folder you should definetely try my second method and creating a bash script in /usr/bin or usr/local/bin and from there call your file by its absolute path.
@AlexLucaci I like your second option too, but I will use rsp's solution. I don't know why but it is more trustful to use the npm install command.
@ballangddang whatever suits your need :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.