npm command 'serve ' not found, although it is installed - linux

I have installed serve with npm as "npm install serve -g" and also with yarn "yarn global add serve", but when I try to run "serve -s build" it says that "Command 'serve' not found.

You should not install the packages globally.Try to do the following-
npm uninstall -g serve
npm i -S serve
Let me know if this works.

I had same problem too and this helped me to fix it so try this after installing serve;
npx serve -s build
or
npx serve -s build -p 8000
(8000 = it depends by your choice)
I don't know why but this worked for me

None of these above answers worked for me, so this is what works for me :
sudo su
npm install -g serve
Installing as root helps globally installing serve

Make sure to have this in your .bashrc or .zshrc
if you're using Yarn:
export PATH="$PATH:$(yarn global bin)"
if you're using NPM:
export PATH="$(npm bin -g):$PATH"
So that the shell would know where to look for executables such as serve, npx, live-server etc that are installed globally.
Make sure to reload your shell config:
source ~/.bashrc // or ~/.zshrc

If anyone still gets the problem, try this:
npm uninstall -g serve
npm i -S serve
yarn global add serve

I faced the same problem, what I did was run the command yarn serve -s build
If you got it installed with npm then you can just add npm before the suggested command

Related

'express' is not recognized as an internal or external command

I am trying to start an express-generator app but I am getting "express is not recognized an internal or external command. I tried all the below
in windows CMD as administrator:
$ npm install express -g
$ npm install express-generator -g
$ mkdir myApp
$ cd myApp
$ express helloApp
the latest command was supposed to create all the express-generator related files (i.e. routes, models, views, etc.). But I am still getting the same error again and again. Any solutions would be much appreciated.
Since you are using npm < v8.2.0 you need to install it globally (v8.2.0 allows you to use npx). From their documentation:
$ npm install -g express-generator
$ express --view=pug myApp
You can change the view parameter or even exclude it. This is how I always do it and I just tested it to confirm it works for me although I am using a different version of npm and node than you are.
try this.
npm cache clear --force
$ npm install -g express-generator
$ mkdir myApp
$ cd myApp
$ express helloApp `
remember if you are installing globally you need to use sudo for linux and mac, and powershell or admin cmd to run the code. eg
$ sudo npm install -g express-generator
i solved the above error by uninstalling node js(v10.16.3) and reinstalling the current version (v12.12.0) ....followed by the known commands:
npm install express -g
npm install express-generator -g
express project_folder_name
honestly till now i couldn't find any reason for what caused this...but i suppose it was something related to my node_modules directory....
thanks Dillan and Ani for your kind help...your suggestions helped a lot....thanks :)
One other possibility - I was unable to use express-generator in vscode. I tried to use it separately in both cmd and powershell. It worked using cmd, but not powershell. Modify vscode to make cmd the default terminal and you're good!

Nodemon installed but can't be found

I've tried uninstalling and and reinstalling nodemon several times both locally and globally with:
npm install -g nodemon
(tried it both with and without sudo)
and it seems to install no problem, and gives me:
/usr/local/bin/bin/nodemon -> /usr/local/bin/lib/node_modules/nodemon/bin/nodemon.js
/usr/local/bin/lib
└── nodemon#1.11.0
but whenever I run
nodemon server.js
in my app, I get;
-bash: nodemon: command not found
Like I mentioned, I've tried the same process but installing locally to my app dependancies, but it doesn't seem to make a difference. What's going on here? I followed the same process on a different machine, and it worked no problem.
Googling around, I came across some posts that mentioned changing/adding the PATH? But it's not clear to me if that's or the problem or what that means.
Also, other globally installed npm modules run just fine
nodemon is not being found by bash.
Edit your ~/.bash_profile file and add:
PATH=$PATH:/usr/local/bin/bin/
Start a new shell to see it work, or run source ~/.bash_profile to have it apply to the current session.
Instead of using sudo switched as root and then just run:
$ npm install -g nodemon
sudo su -
export PATH=$PATH:/home/USER/npm
npm install -g --force nodemon
# THESE LINES + START FROM A NEW TERMINAL...
# IN MY CASE
npm install -g --force node-inspector
# TOO

Node-sass is not recognized by command line

I'm trying to set up node-sass, following the instructions on CSS-Tricks. Node and npm are installed correctly, and the node-sass installation worked too. When I go to run node-sass --output-style compressed -o dist/css src/scss, though, I get an error message stating
'node-sass' is not recognized as an internal or external command,
operable program or batch file.
I've done a fair bit of Googling and searched Stack Overflow directly. My question isn't about "node" not being recognised as a command. I know node is working as I can run node -v and npm -v, and node-sass was successfully installed after running npm install --save-dev node-sass (there's a folder in node_modules) and no errors appeared in the command line.
Other information: I am running Windows 10 and just did a clean install of node and npm before trying to use node-sass.
EDIT: I uninstalled and reinstalled with -g thanks to #Bhavik's suggestion, and it's now working
You need to install it globally
npm install -g node-sass
Or add it in package.json
"devDependencies": {
"node-sass": "4.5.0"
},
"scripts" : {
"node-sass": "node-sass --output-style compressed -o dist/css src/scss"
}
And then do
1. npm i, which in this case would be similar to npm install --save-dev node-sass
2. npm run node-sass
Reference: npm scripts, npm-run-scripts
You can simply run this code
npm install -g sass
sass --watch sass:css
Hopefully work
npm commands check "node_package" folder and try to run things there. You can try
npx run scss
to install scss and then run it, even if it is not installed before.
The below solves the problem
yarn global add node-sass-chokidar
node-sass v4.13+
Install node-sass in your project locally
cd <root path of your project>
yarn add -D node-sass
// or
npm install -D node-sass
Add a script to your package.json
"scripts" : {
...
"compile:sass": "node-sass --recursive --watch <sass directory> --output <css directory>",
...
}
Run the script from the command line
yarn compile:sass
// or
npm run compile:sass
This is a simple problem don't worry too much. Just go to package.json file and add this code
"devDependencies": {
"node-sass": "4.9.2"
},
"scripts" : {
"node-sass": "node-sass --output-style compressed -o dist/css/ scss --recursive"
}
and just save the file.
And run this command,
npm run node-sass
That's all
First, run npm install -g node-sass as others have pointed out.
Now, the target of the command (sass.cmd) is not located in the current working directory. For it to still be able to run, its location must be in your PATH (or Path) environment variable.
For me, the path is: C:\Users\Guy\AppData\Roaming\npm
Make sure to restart any terminal/IDE you were trying to run it in before trying again. Otherwise it won't recognize the new environment variable.

npm installing dev dependencies on production

I set NODE_ENV to production and tried to install dependencies using a Capfile which contains this:
run "cd #{latest_release} && npm config set production=true && npm install --production"
or this:
run "cd #{latest_release} && npm install --production"
but I always get also the dev dependencies, which is annoying because after a few releases all the inodes are taken and I cannot create any other files on the deploy machine.
I set the environment variable like this in the Capfile:
set :default_environment, {
'NODE_ENV' => 'production'
}
run "echo $NODE_ENV"
and it echoes the correct value.
If I execute
npm install --production
from within a shell, it works correctly. The user that makes the capistrano deploy and this shell user are the same, so I'm quite lost. Any hints?
The problem was:
sudo npm link
which I ran after the install command and which installs all dependencies. The fix is:
sudo npm link --production

I can´t install nodemon globally, "nodemon" not recognized

I want to use nodemon for monitoring my node.js app's, then I execute the next line command:
npm install -g nodemon
or
npm install nodemon -g
When I move to my app folder and try to to
nodemon app.js
The system tells to the next:
"nodemon 'is not recognized as an internal or external command, program or batch file.
Since node prefix is not in the PATH ENV variable , any of the globally installed modules are not getting recognized.
Please try this.
Open cmd prompt
npm config get prefix
append the resulting path to PATH env variable.
Now you should be able to run nodemon from any location.
This is what i have done on my local machine
C:\>npm config get prefix
C:\Users\username\AppData\Roaming\npm
C:\>set PATH=%PATH%;C:\Users\username\AppData\Roaming\npm;
C:\>nodemon
31 Jul 22:30:29 - [nodemon] v0.7.8
31 Jul 22:30:29 - [nodemon] to restart at any time, enter `rs`
31 Jul 22:30:29 - [nodemon] watching: C:\
31 Jul 22:30:29 - [nodemon] starting `node `
^CTerminate batch job (Y/N)? Y
I also got same error as you with this command:
$ sudo npm install -g nodemon
I just really switched as "root" and then just ran:
$ npm install -g nodemon
I think npm has a bug to not work with sudo, but it works fine when you are really "root".
Single line solution
In terminal
npm install -g --force nodemon
There is a problem with integrated terminal of vs code. when I try in external terminal nodemon works. But in integrated terminal, it gives bash: nodemon: command not found error.
so here is my solution
install nodemon as development dependency
npm install --save-dev nodemon
and change package.json of the project
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"nodemon": "./node_modules/.bin/nodemon"
},
to run nodemon type into terminal in project folder
npm run nodemon
Mine was I went to Control Panel and Repair the NodeJS app and tried to install again with npm install -g nodemon and now it works. Maybe you mixed up or something with Node.
check out here :-
npm install -g nodemon
and then run
$nodemon server.js
You won't need to install nodemon anymore, since Nodejs has finally introduced its --watch feature which restarts the process when an imported file is changed.
node --watch index.js
https://nodejs.org/en/blog/release/v18.11.0/
Linux users: I would highly suggest not using sudo or root user to install npm packages. This could become a security problem especially on a production system. I would also suggest not trying to hack permissions as I have hosed an Ubuntu system by not reading the warning on the npmjs procedure.
It would be better to configure npm to use a folder owned by the current user. Simplest approach
wget https://raw.githubusercontent.com/pcnate/npm-configure/master/add-npm-global.sh -q -O - | bash
npm install -g nodemon
Or get the code script on github to see how it works
See details on the npmjs website
On Windows, I was having issues installing nodemon directly from the Command line. Downloaded Cygwin and I was able to npm install nodemon instantly.
You can add path to node packages in System Path variable.
Add "C:\Users\UserName\AppData\Roaming\npm".
Even after adding path to System Path variable it did not work for me using nodemon. Then i used npm run serve to run the server. now it is up and running. Btw i am a windows user
This command worked for me.
If your global installation didn't work then install it in your
development dependency.
npm install --save-dev nodemon
Updated
After Path settings we also need to type in the following commands
Set-ExecutionPolicy Unrestricted
what this command enables running scripts on the system
I think some of us can't reach global environments without admin privileges.
If you tried everything and it's still not working, try running VSCode as administrator. It worked out for me.
had the same problem otherwise was just working fine a day ago.
Very simple fix
first check if nodemon exists on your system globally or not
To check
npm list -g --depth=0
If you don't see then install
it npm install -g nodemon (g stands for globally)
If you see it still doesn't work then you need to configure environment variable
I use Windows OS. On Windows navigate to
Control panel>System>Advanced System Settings>Environment Variables>double-click on PATH
Now check if you have this PATH C:\Users\yourUsername\AppData\Roaming\npm
If not, you will see some existing paths, just append to it separating with semicolon. That's it! Worked for me.
For me node was installed in C:..\Roaming\npm and for you if the PATH is different, you will put in whatever applcable.

Resources