Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difficulty solving "sh: <npm package name> command not found" with npm run <npm package name>

Tags:

npm

I've been adding npm modules to my project for the first time (jshint, optimg, jpgo). I notice that some projects, when I do npm run [name], give a result of "sh: [name]: command not found."

I can't figure out why those don't work, but the other npm installs do. All these are installed locally; I can see the install by looking in the /node_modules folder in my project root and verify them with npm ls.

The latest package that gets this error is html-minify. My package.json looks like this (and validates at 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"

    }
}

I tried "html-minify **.html" with the same resulting "not found" error.

Why would I get "sh: [npm package name]: command not found"? I've read the other threads, and because the other modules work, I doubt that I need to add anything to my PATH, or start a server, or install globally.

Fuller error message (for 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
like image 734
Steve Avatar asked Feb 24 '15 16:02

Steve


1 Answers

so short answer to run npm package commands locally

npm i install cowsay -d

then use

npx cowsay I am working locally

output should be

______
< I am working locally  >
 ------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

on the other hand installing npm packages globaly on linux has problems with addministrator and node-module location

so best practice to get global packges to work normally is to install NVM first

sudo apt install curl
curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash
source ~/.profile

then once nvm is installed install from nvm node

nvm install node 

or a specific version

nvm install 12.18.3

now installing global packages and running them is simple

npm install -g cowsay

cowsay I am working globaly

will output

______
< I am working  >
 ------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

now global packages are working fine and local packages can be run from the directory of the project using npx.

like image 78
karim samir Avatar answered Oct 25 '22 13:10

karim samir