Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Command not found after npm link

I have a small node.js app "doto" that I want to npm link, so that I can just call doto anywhere. As of my understanding all I need to do is:

mkdir doto
cd doto
npm init #call the project doto and entry point doto.js
touch doto.js #fill with some code
npm link

node doto.js works just fine, but when I link the package and try to call doto, the command is not found. The linking went fine, I had to use sudo (yes I know I should setup node a way that I do not need sudo, but for now I just want to get my feet wet)

Whenever I install a package globally, I can call it just fine.

I am running mac os 10.10.

doto.js

#!/usr/bin/env node

var path = require('path');
var pkg = require( path.join(__dirname, 'package.json') );

var program = require('commander');

program
    .version(pkg.version)
    .option('-p, --port <port>', 'Port on which to listen to (defaults to 3000)', parseInt)
    .parse(process.argv);

console.log(program.port);

package.json

{
  "name": "doto",
  "version": "0.0.1",
  "description": "",
  "main": "doto.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "commander": "~2.7.1"
  }
}

What am I missing?

like image 491
rootman Avatar asked Oct 18 '25 13:10

rootman


1 Answers

I think your package.json is missing the bin section, according to the docs it should become something like:

{
  "name": "doto",
  "version": "0.0.1",
  "description": "",
  "main": "doto.js",
  // specify a bin attribute so you could call your module
  "bin": {
    "doto": "./doto.js"
  },
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "commander": "~2.7.1"
  }
}

So after you've run sudo npm link you can run doto from anywhere, if you want to change the name of the executable just change the key under "bin" to whatever you prefer.

like image 163
Alberto Zaccagni Avatar answered Oct 21 '25 01:10

Alberto Zaccagni