itinance
itinance

Reputation: 12418

How to create a nodejs-command-line-tool that it can be installed globally?

How to build/install a node-command-line-tool that it can be used globally?

I wrote a little command-line-utility called "extractdeps", that will list all dependencies from a node-application out of a package.json-file.

Unlike other node-utilities like "jest" i am unable to execute it globally in shell without to enter its concrete path.

Example of installation:

$ npm install --verbose -g extractdeps -g
npm info it worked if it ends with ok
npm verb cli [ '/usr/local/Cellar/node/8.9.1/bin/node',
npm verb cli   '/usr/local/bin/npm',
npm verb cli   'install',
npm verb cli   '--verbose',
npm verb cli   '-g',
npm verb cli   'extractdeps',
npm verb cli   '-g' ]
npm info using [email protected]
npm info using [email protected]
npm verb npm-session 5cff952d9a7f0be7
npm http fetch GET 200 https://registry.npmjs.org/extractdeps 19ms (from cache)
npm http fetch GET 200 https://registry.npmjs.org/co 14ms (from cache)
npm http fetch GET 200 https://registry.npmjs.org/commander 16ms (from cache)
npm http fetch GET 200 https://registry.npmjs.org/fs 15ms (from cache)
npm http fetch GET 200 https://registry.npmjs.org/shelljs 16ms (from cache)
npm http fetch GET 200 https://registry.npmjs.org/glob 6ms (from cache)
npm http fetch GET 200 https://registry.npmjs.org/interpret 8ms (from cache)
npm http fetch GET 200 https://registry.npmjs.org/rechoir 8ms (from cache)
npm http fetch GET 200 https://registry.npmjs.org/fs.realpath 20ms (from cache)
npm http fetch GET 200 https://registry.npmjs.org/inflight 21ms (from cache)
npm http fetch GET 200 https://registry.npmjs.org/once 20ms (from cache)
npm http fetch GET 200 https://registry.npmjs.org/minimatch 21ms (from cache)
npm http fetch GET 200 https://registry.npmjs.org/inherits 22ms (from cache)
npm http fetch GET 200 https://registry.npmjs.org/path-is-absolute 21ms (from cache)
npm http fetch GET 200 https://registry.npmjs.org/wrappy 5ms (from cache)
npm http fetch GET 200 https://registry.npmjs.org/brace-expansion 4ms (from cache)
npm http fetch GET 200 https://registry.npmjs.org/balanced-match 5ms (from cache)
npm http fetch GET 200 https://registry.npmjs.org/concat-map 5ms (from cache)
npm http fetch GET 200 https://registry.npmjs.org/resolve 3ms (from cache)
npm http fetch GET 200 https://registry.npmjs.org/path-parse 4ms (from cache)
npm verb correctMkdir /Users/hagen/.npm/_locks correctMkdir not in flight; initializing
npm verb lock using /Users/hagen/.npm/_locks/staging-3a08f0df5026584d.lock for /usr/local/lib/node_modules/.staging
npm info lifecycle [email protected]~preuninstall: [email protected]
npm info lifecycle [email protected]~uninstall: [email protected]
npm verb unbuild rmStuff [email protected] from /usr/local/lib/node_modules
npm info lifecycle [email protected]~postuninstall: [email protected]
npm info lifecycle [email protected]~preinstall: [email protected]
npm info linkStuff [email protected]
npm verb linkBins [email protected]
npm verb linkMans [email protected]
npm info lifecycle [email protected]~install: [email protected]
npm info lifecycle [email protected]~postinstall: [email protected]
npm verb unlock done using /Users/hagen/.npm/_locks/staging-3a08f0df5026584d.lock for /usr/local/lib/node_modules/.staging
+ [email protected]
updated 1 package in 0.764s
npm verb exit [ 0, true ]
npm info ok

But executing fails:

$ extractdeps
-bash: extractdeps: command not found

If i do the same with other tools like jest, it works perfectly:

$ jest
-bash: jest: command not found

$ npm install -g jest
/usr/local/bin/jest -> /usr/local/lib/node_modules/jest/bin/jest.js

$ jest -v
No tests found

What did i missed here?

Upvotes: 0

Views: 237

Answers (1)

Jack Ryan
Jack Ryan

Reputation: 1318

So, I just installed the package globally and noticed the same deal. I am wondering if you need to specify a "bin" flag in your package.json. It looks like it is installing the module but it does not know what to add to the /usr/local/bin.

For example, in the grunt-cli package.json:

"bin": {
    "grunt": "bin/grunt"
 },

Update: did some research and that indeed looks to be the issue. https://docs.npmjs.com/files/package.json#bin

Basically, when you specify this flag and do a global install, npm creates a symlink in your local bin to whichever cli entrypoint you specified.

Upvotes: 1

Related Questions