npm包的package.json定义了bin模块,但是命令找不到。

6

我在定义的 package.json 文件中遇到了一个与 bin 模块相关的 command not found... 问题。 为什么会这样呢? 我以为它应该自动将本地命令映射到路径中。

在我的模块的 package.json 文件中:

  "bin": {
    "testme": "./misc/testme"
  },

./misc/testme script:

#!/usr/bin/env node
console.log("this is a test");

它出现在node_modules/.bin目录中。
$ ls node_modules/.bin
acorn       escodegen    gulp        kue-dashboard  ncp                 semver               stylus
cake        esgenerate   gzip-size   lessc          nopt                shjs                 testme
cleancss    esparse      handlebars  make-plural    pretty-bytes        sshpk-conv           uglifyjs
coffee      esvalidate   image-size  messageformat  rc                  sshpk-sign           user-home
dateformat  express      jade        mime           retrieve-arguments  sshpk-verify         uuid
dot-object  geojsonhint  jsonlint    mkdirp         rimraf              strip-indent         watchr
errno       grunt        js-yaml     mustache       sails               strip-json-comments  which

但是,在执行npm install之后,当我运行它时,会得到以下错误信息:
$ testme
bash: testme: command not found...
3个回答

6

对于我来说,在Windows上,执行以下操作:

在项目根文件夹中运行npm link

然后再执行命令。我不知道其他操作系统是否也适用。


1
它在Linux Mint上也可以运行。谢谢! - O.k

6

我认为只有在全局安装了该软件包后,才能运行testme命令。如果不进行全局安装,您需要使用npm run testme命令,并将其添加到您的package.json文件中:

"scripts": {
    "testme": "./bin/testme"

更多信息请查看:http://2ality.com/2016/01/locally-installed-npm-executables.html

这篇文章介绍了如何在本地安装npm可执行文件。

2
有两种方法(我能想到的)来运行这个命令:-
a(由@TAMAS告知)。
 npm install -g testme 

b.

 PATH = $PATH:/your/project/dir/node_modules/.bin
    
 EXPORT PATH

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接