难以使用npm run <npm package name>解决"sh:<npm package name> command not found"

Ste*_*eve 6 npm

我第一次将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"

    }
}
Run Code Online (Sandbox Code Playgroud)

我尝试了"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
Run Code Online (Sandbox Code Playgroud)

Dhr*_*hok -1

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

建议使用 -g 安装命令:

npm install html-minifier -g
Run Code Online (Sandbox Code Playgroud)

然后运行 ​​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
Run Code Online (Sandbox Code Playgroud)

另外,对于您正在观察的脚本问题(其中运行 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"
   }
Run Code Online (Sandbox Code Playgroud)

}

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

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

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

安装说明

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

npm install html-minifier -g
Run Code Online (Sandbox Code Playgroud)

来自 NPM 以供编程使用:

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
Run Code Online (Sandbox Code Playgroud)

来自 Git:

 {
   "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"
   }
Run Code Online (Sandbox Code Playgroud)

用法

请注意,默认情况下几乎所有选项都处于禁用状态。有关命令行用法,请参阅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

npm install html-minifier -g
Run Code Online (Sandbox Code Playgroud)