最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Command not found after npm link - Stack Overflow

programmeradmin2浏览0评论

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 mand 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('mander');

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": {
    "mander": "~2.7.1"
  }
}

What am I missing?

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 mand 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('mander');

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": {
    "mander": "~2.7.1"
  }
}

What am I missing?

Share Improve this question asked Mar 19, 2015 at 11:05 rootmanrootman 6701 gold badge9 silver badges18 bronze badges 1
  • I do not really understand what happened in my case. The call to the linked package didn't work from my ubuntu terminal instance. I opened another terminal instance inside of vs-code which I was using to develop the package and ran the mand in it, this time, it worked. – xodeeq Commented Jun 6, 2024 at 8:56
Add a ment  | 

2 Answers 2

Reset to default 8

I think your package.json is missing the bin section, according to the docs it should bee 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": {
    "mander": "~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.

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.

发布评论

评论列表(0)

  1. 暂无评论