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