【问题标题】:Difficulty solving "sh: <npm package name> command not found" with npm run <npm package name>使用 npm run <npm package name> 解决“sh: <npm package name> command not found”的困难
【发布时间】:2015-04-26 10:15:48
【问题描述】:

我第一次将 npm 模块添加到我的项目中(jshint、optimg、jpgo)。我注意到一些项目,当我执行 npm run [name] 时,会给出“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 中的任何内容,或启动服务器,或全局安装。

更完整的错误消息(用于 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

【问题讨论】:

  • Ubuntu 14.04.5 LTS(GNU/Linux 3.13.0-110-generic x86_64),节点 6.10.0,npm 3.10.10。同样的问题。

标签: npm


【解决方案1】:

在本地运行 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 从项目目录运行。

【讨论】:

    【解决方案2】:

    对于您在 html-minifier 中看到的 command not found 问题,这可能与未全局安装 npm 包(使用 -g)或使用错误名称调用命令有关(我相信您在调用当包定义为 html-minifier 时的包名 html-minify)

    建议使用 -g 安装命令:

    npm install html-minifier -g
    

    然后运行 ​​html-minifier 命令:

    html-minifier --help (to test installation)
    html-minifier --collapse-whitespace --remove-comments --remove-optional-tags --remove-redundant-attributes --remove-script-type-attributes --remove-tag-whitespace --use-short-doctype --minify-css true --minify-js true
    

    对于您正在观察的脚本问题(运行 npm run html 时返回的命令未找到) - 这是一个示例 package.json。我运行 npm run html (在使用上述步骤全局安装 html-minifier 之后)并运行 html-minifier --help:

     {
       "name": "stackoverflow",
       "version": "1.0.0",
       "description": "",
       "main": "index.js",
       "scripts": {
         "test": "echo \"Error: no test specified\" && exit 1",
         "html": "html-minifier --help"
       },
       "author": "",
       "license": "ISC",
       "dependencies": {
         "html-minifier": "^4.0.0"
       }
    

    }

    Github 链接:https://github.com/kangax/html-minifier

    npm 链接:https://www.npmjs.com/package/html-minifier

    README 中提供的安装和使用步骤:

    安装说明

    从 NPM 用作命令行应用程序:

    npm install html-minifier -g
    

    来自 NPM 以供编程使用:

    npm install html-minifier
    

    来自 Git:

    git clone git://github.com/kangax/html-minifier.git
    cd html-minifier
    npm link .
    

    用法

    请注意,默认情况下几乎所有选项都处于禁用状态。有关命令行用法,请参阅html-minifier --help 以获取可用选项列表。试验并找出最适合您和您的项目的方法。

    • 示例命令行: html-minifier --collapse-whitespace --remove-comments --remove-optional-tags --remove-redundant-attributes --remove-script-type-attributes --remove-tag-whitespace --use-short-doctype --minify-css true --minify-js true

    Node.js

    var minify = require('html-minifier').minify;
    var result = minify('<p title="blah" id="moo">foo</p>', {
      removeAttributeQuotes: true
    });
    result; // '<p title=blah id=moo>foo</p>'
    

    【讨论】:

      猜你喜欢
      • 2018-07-18
      • 1970-01-01
      • 2021-05-03
      • 2018-10-14
      • 2016-03-25
      • 2019-08-20
      • 2020-01-28
      • 2022-01-28
      • 1970-01-01
      相关资源
      最近更新 更多