Reputation: 670
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?
Upvotes: 4
Views: 3019
Reputation: 3077
I tried npm link
and it still was not working from my test package.
My package.json
in the linked package had "directories": { "bin": "./bin" }
instead of "bin": { "etc": "./etc.js" }
.
Once I changed it back to "bin": {...}
it started to work in the test package.
So, although this directories: { bin: ... }
setting is documented it doesn't seem to work correctly with npm link
.
Upvotes: 0
Reputation: 31560
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.
Upvotes: 8