npm链接后找不到命令

时间:2023-01-17 22:15:02

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:

我有一个小node.js应用程序“doto”我想要npm链接,所以我可以在任何地方调用doto。根据我的理解,我需要做的是:

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)

node doto.js工作正常,但是当我链接包并尝试调用doto时,找不到该命令。链接很顺利,我不得不使用sudo(是的,我知道我应该设置节点,我不需要sudo,但是现在我只想弄湿我的脚)

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

每当我在全球范围内安装软件包时,我都可以称之为正常。

I am running mac os 10.10.

我正在运行mac os 10.10。

doto.js

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

的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?

我错过了什么?

1 个解决方案

#1


3  

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

我认为你的package.json缺少bin部分,根据文档它应该变成类似的东西:

{
  "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.

因此,在运行sudo npm链接之后,您可以从任何地方运行doto,如果您想更改可执行文件的名称,只需将“bin”下的密钥更改为您喜欢的任何内容。

#1


3  

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

我认为你的package.json缺少bin部分,根据文档它应该变成类似的东西:

{
  "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.

因此,在运行sudo npm链接之后,您可以从任何地方运行doto,如果您想更改可执行文件的名称,只需将“bin”下的密钥更改为您喜欢的任何内容。