在npm链接后找不到命令

Command not found after npm link

本文关键字:找不到 命令 链接 npm      更新时间:2023-09-26

我有一个小的node.js应用程序"doto",我想要npm link,这样我就可以在任何地方调用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工作正常,但当我链接包并尝试调用doto时,找不到命令。链接进行得很好,我不得不使用sudo(是的,我知道我应该以一种不需要sudo的方式设置节点,但现在我只想弄湿我的脚)

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

我正在运行macos10.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);

软件包.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"
  }
}

我错过了什么?

我认为您的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"
  }
}

因此,在运行sudo npm link之后,您可以在任何地方运行doto,如果您想更改可执行文件的名称,只需将"bin"下的键更改为您喜欢的任何键即可。

我尝试了npm link,但它在我的测试包中仍然不起作用。

链接包中的package.json具有"directories": { "bin": "./bin" },而不是"bin": { "etc": "./etc.js" }

一旦我把它改回"bin": {...},它就开始在测试包中工作了。

因此,尽管directories: { bin: ... }设置有文档记录,但它似乎无法正确使用npm link