1

scenario:

I have written a cli script. It lives in directory mycli and is called mycli.js and is shebanged as #!/usr/bin/env node. In the project's package.json I have included "name": "mycli", "bin": "mycli.js".

At this point I can call mycli from the project root two ways, either as $ node mycli or simply $ mycli.js.

After publishing to npm and installing this package globally, I can now call $ mycli or $ node mycli from anywhere, however, a local install doesn't exhibit the same behavior from its own project root; I have to call the script using the more qualified name, either as $ node_modules/mycli/mycli.js, or $ node node_modules/mycli/mycli.

Why doesn't $ node mycli work in the local scenario?

Lokua
  • 576
  • 5
  • 15
  • Which OS? Directory `mycli` where? What's the actual package.json? Did you npm publish this or is it installed locally from a repo url? (which one? how did you install it?) why does running `mycli.js` work, given that .js files are in no way automatically executable? More exact details, less assuming we know what you're talking about when it comes to your setup. – Mike 'Pomax' Kamermans Sep 10 '14 at 05:23
  • edited. perhaps that clears it up. – Lokua Sep 10 '14 at 05:30
  • if you've used npm publish, what's the package, because that'll help others help you. That said, what do you mean with "local" install? `npm install ... --save` so that it can be used as module? Because if so, that's simply how node modules work. They don't register as global executable unless globally installed. – Mike 'Pomax' Kamermans Sep 10 '14 at 05:31
  • https://www.npmjs.org/package/giftwrap – Lokua Sep 10 '14 at 05:38
  • Yes - local, as in without the -g :) I'm wondering why the local doesn't behave like a local executable when node has put it in the local .bin https://www.npmjs.org/doc/files/package.json.html#bin – Lokua Sep 10 '14 at 05:42

1 Answers1

3

Check out the following answer on: How to use package installed locally in node_modules?

Basically, when you install a package globally, the executable will be found because it's in your PATH. The node_modules directory isn't in your PATH by default. Adding every node_modules directory on your system to your PATH would get quite messy.

You can check your current PATH with echo $PATH. You can add your local node_modules directory (for the lifetime of your session) with PATH=$(npm bin):$PATH. The linked answer also has an alternative using aliases.

Community
  • 1
  • 1
RickN
  • 12,537
  • 4
  • 24
  • 28