npm - 用 npm run <npm package name> 解决 "sh: <npm package name> command not found"的困难

标签 npm

我第一次将 npm 模块添加到我的项目中(jshint、optimg、jpgo)。我注意到有些项目,当我做 npm 运行 [名称] ,给出“sh: [name]: command not found”的结果。

我不知道为什么这些不起作用,但其他 npm 安装可以。所有这些都安装在本地;我可以通过查看项目根目录中的/node_modules 文件夹来查看安装,并使用 进行验证。 npm ls。

出现此错误的最新软件包是 html-minify。我的 package.json 看起来像这样(并在 http://jsonlint.com/ 处验证):

{
    "name": "npmTest",
    "devDependencies": {
        "jshint": "latest",
        "optimg": "latest",
        "jpgo": "latest",
        "ycssmin": "latest",
        "html-minify": "latest"
    },
    "scripts": {
        "lint": "jshint **.js",
        "png": "optimg",
        "jpg": "jpgo",
        "css": "ycssmin **.css",
        "html": "html-minify"

    }
}

我尝试了“html-minify **.html”,但结果是“未找到”错误。

为什么我会得到“sh: [npm package name]: command not found”?我已经阅读了其他线程,并且由于其他模块可以正常工作,我怀疑我是否需要在我的 PATH 中添加任何内容,或者启动服务器,或者全局安装。

Fuller 错误消息(用于 html5-lint):
$ npm run html

> npmTest@ html /Users/Steve/Documents/APPS/Test_Apps/npmTest
> html5-lint

sh: html5-lint: command not found

npm ERR! npmTest@ html: `html5-lint`
npm ERR! Exit status 127
npm ERR! 
npm ERR! Failed at the npmTest@ html script.
npm ERR! This is most likely a problem with the npmTest package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     html5-lint
npm ERR! You can get their info via:
npm ERR!     npm owner ls npmTest
npm ERR! There is likely additional logging output above.
npm ERR! System Darwin 14.1.0
npm ERR! command "node" "/usr/local/bin/npm" "run" "html"
npm ERR! cwd /Users/Steve/Documents/APPS/Test_Apps/npmTest
npm ERR! node -v v0.10.25
npm ERR! npm -v 1.3.24
npm ERR! code ELIFECYCLE
npm ERR! 
npm ERR! Additional logging details can be found in:
npm ERR!     /Users/Steve/Documents/APPS/Test_Apps/npmTest/npm-debug.log
npm ERR! not ok code 0

最佳答案

在本地运行 npm 包命令的简短回答

npm i install cowsay -d
然后使用
npx cowsay I am working locally
输出应该是
______
< I am working locally  >
 ------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||
另一方面,在 linux 上全局安装 npm 包存在 addministrator 和节点模块位置的问题
所以让全局包正常工作的最佳实践是先安装 NVM
sudo apt install curl
curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash
source ~/.profile
然后一旦安装了 nvm 从 nvm 节点安装
nvm install node 
或特定版本
nvm install 12.18.3
现在安装全局包并运行它们很简单
npm install -g cowsay

cowsay I am working globaly
将输出
______
< I am working  >
 ------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||
现在全局包工作正常,本地包可以使用 npx 从项目目录运行。

关于npm - 用 npm run <npm package name> 解决 "sh: <npm package name> command not found"的困难,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28701753/

相关文章:

node.js - 获取所有包含某个关键字的npm包

node.js - 命令gulp错误 "assert.js:42 throw new errors.AssertionError"

node.js - CentOS 7 上全局 npm 安装的理想目录

meteor - `npm install` 是否与 Meteor 1.3 中的 `meteor npm install` 相同?

node.js - Reactjs:意外的 token

node.js - 在哪里可以找到 npm 包的文档?

node.js - 在 Phonegap/Cordova 项目中使用 Node 包

javascript - yarn 与 Npm - "works on my machine"- 澄清?

node.js - 使用 Symfony 和 Assetic 则更少。 node_modules 的权限所以我不需要 sudo?

Node.js 权限被拒绝公钥