gpt4 book ai didi

javascript - 在 npm 链接后找不到命令

转载 作者:搜寻专家 更新时间:2023-11-01 00:18:00 25 4
gpt4 key购买 nike

我有一个小型 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 的方式设置 Node ,但现在我只想尝试一下)

每当我全局安装一个包时,我都可以调用它。

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

包.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 部分,according to the docs它应该变成这样的:

{
"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”下的键"随心所欲。

关于javascript - 在 npm 链接后找不到命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29142922/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com