"npm run" commands fail on Windows - node.js

I am using Windows PowerShell ISE with administrator privileges. My project uses webpack, which is installed as a local dependency with npm. I have a "script" defined in package.json:
"build": "webpack --progress --config resources/assets/build/webpack.config.js",
When I execute npm run build I get the error message 'webpack' is not recognized as an internal or external command, but if I copy the webpack command and execute that it works fine. I'm not going to list my whole system PATH here, but it includes .\node_modules\.bin at the beginning. Clearly Windows has no problem finding webpack but Node for some reason can't.
I tried running npm run build in a privilege-less Command Line, no difference. I upgraded to the latest versions of Node and npm (13.0.1 and 6.12.1, respectively), to no avail. I even tried prefixing the command in package.json with ./node_modules/.bin/ but that didn't work.
Please don't suggest that I install webpack globally as that is not a real solution. Node is giving me this error with all locally-installed commands, not just webpack. I should be able to run commands locally through npm.

if you're using a recent version of npm, you can try
npx webpack ....
alternatively, you can install webpack locally and specify the exact path
node_modules/webpack/bin/webpack.js ....

In the end, I had to bite the bullet and install webpack globally.

Related

Vite: why am I getting vite:command not found error?

I have installed vite in my vue.js app. I start the app by typing npm run dev in the main project directory. In the package.json this is defined as:
"dev": "vite"
but if I try do run this command (or eg. vite build) 'manually' from main directory, I get an error:
bash: vite: command not found
I also figured out that when I set a new script:
"build": "vite build"
I can run this command also, although, again, running it manually will result in error as above.
This seems quite illogical to me. Can anybody explain how is it possible?
If you didn't install vite globally (using npm install -g), then the vite command will be in node_modules/.bin in your main directory. The npm run command temporarily adds that folder to your PATH so it can execute that command. Try running ./node_modules/.bin/vite to run it without npm.

"vite is not recognized ..." on "npm run dev"

I'm using Node.js and npm for the first time, I'm trying to get Vite working, following the tutorials and documentation. But every time I run into the problem 'vite' is not recognized as an internal or external command, operable program or batch file. I have been trying to find a solution for 4 hours now but with no results.
I tried restarting pc, reinstalling node.js, several procedures to create vite project but in vain. I suppose it's my beginner's mistake, but I really don't know what to do anymore.
Commands and responses I run when I try to create a vite project:
npm create vite#latest
>> my-portfolio >> vanilla & vanilla
cd my-portfolio
npm install >>resp: up to date, audited 1 package in 21s found 0 vulnerabilities
npm run dev
resp:
> my-portfolio#0.0.0 dev
> vite
'vite' is not recognized as an internal or external command,
operable program or batch file.
try to install the packages to make it work
npm install or npm i
For this error use the following command on your terminal in the present working directory of the project
npm install
npm run dev
first, try to install a node package manager and then run npm run dev hope it will work
yarn add vite
on project folder to add vite,
and run
npm run dev
again.
remember to update your node version to 18, LTS from 17 might not support this installation.
update:
I try to fresh install again my Laravel 9.19, since i had update my node to version 18, npm install & npm run dev just work fine without yarn.
According to documentation https://vitejs.dev/guide/#community-templates
npm install
npm run dev
npx vite build
I found myself in the same situation.
The problem is vite.cmd is not in the system or user PATH variable, so it cannot be found when it is executed from your project folder.
To fix it, you should temporarily add the folder where vite.cmd is in your PATH variable (either for the entire system or your user). I recommend adding it just for your user, and keep in mind you should probably remove it after you stop working on that project, because this could affect future projects using the same build tools.
To do this:
My PC > Properties > Advanced system settings > Click on Environment Variables (alternatively just use the start button and begin typing Environment, you should get a direct link)
On "User variables" find "Path" and edit it.
Add a new entry for the folder where vite.cmd is. Example "C:\dev\reactplayground\firsttest\test01\node_modules.bin" Check your project folder to find the right path.
Make sure your close and open your console for this change to affect.
Go back to your project root folder and run "vite build", it should work now.
for me I've:
1 - excuted yarn add vite
2- and then npm install
work fine !
For me I had a project I created on one computer and it had this in devDependencies:
"vite": "^3.1.0"
I did pnpm install and it reported everything was fine, but I was getting the error. I ran pnpm install vite and it installed it again with this:
"vite": "^3.1.8"
After that it worked fine. So try using npm, yarn, or pnpm to install the vite package again and see if that works.
try npm install
then npm run build
Recently faced this error and I run
npm install
npm run dev
then the output was
VITE v3.2.4 ready in 1913 ms
THAT'S COOL 😎😎😎
reference LINK
You need Node version 15 or higher, I had the same problem because I was using an older version of it.
Needs to install all the packages in package.json and run again
npm i
npm run dev
For me this worked:
I changed NODE_ENV environment variable to development ( earlier it was production - which should not be the case, as dev-dependencies won't get installed by npm install or yarn )
Here is what to make sure before running npm install or yarn:
Make sure `NODE_ENV` environment variable is not set to `production` if you running locally for dev purpose.
'vite' is not recognized as an internal or external command, operable program or batch file.
> vite
'vite' is not recognized as an internal or external command,
operable program or batch file.
try to install the packages to make it work
npm install or npm i
The following works just fine!
npx vite build
npm i
npm run dev
I had the same challenge and I was finding the error
create-vite : command not found
I resolved by running the command:
npm i create-vite
From there you can continue with running the command:
npm create vite#latest
Reference:
https://www.npmjs.com/package/create-vite
npm install or npm i
helps with issue of 'vite' is not recognized
After I tried npm install
i could run the npm run dev after and it showed me the localhost

npm packages not available when installed locally

I am working with npm on a web app and I found an issue when using some packages that requires terminal commands to run such like nodemon and concurrently
I installed it via
sudo npm install --save-dev nodemon
and when I try to use it via:
nodemon ./server.js
I get an error
nodemon command not found
and the same when I used concurrently
I tried also with
sudo npm install --save nodemon
and it doesn't work.
it only work if I installed it globally
sudo npm install -g nodemon
Why I can't use it when install locally?
Note: I can found the executable file at node_modules/.bin
but this following not working as well
node_modules/.bin/nodemon ./server.js
Global packages can be launched directly because they are saved in your PATH directory by default. If you saved a package locally you can see it on node_modules/.bin/ as you mentioned. So there are 2 ways to achieve what you want if you want to run an executable package if installed locally:
You can run it via terminal as ./node_modules/.bin/nodemon yourscript.js
Or via npm scripts in your package.json file, you do this:
{
"scripts": {
"nodemon": "nodemon yourscript.js"
}
}
and execute npm run nodemon.
The 2nd approach works for both packages installed globally or locally.
I prefer installing packages locally, so my other apps won't get affected especially if I'm using different package versions per project.
UPDATE
On npm#5.2.0 onwards, it comes with a binary called npx. So you can run specific packages on the terminal just by npx [package] and it executes either your local or global npm package. In your case it should be something like npx nodemon server.js.
Because it's in your node_modules/.bin folder, not your PATH.
You can either use ./node_modules/.bin/nodemon or $(npm bin)/nodemon to call nodemon.
To run any locally installed npm module (Mocha, Eslint, Nodemon, etc.), you can now use npx. Try npx nodemon server.js.
I also recommend setting main within your package.json to point to the script you want to run (index.js by default), so you could just run npx nodemon or nodemon (if globally installed) and it will know which script to run.
This is because the local node_modules folder is not in your PATH. See the link to the duplicate question for more details.

webpack command not working

I am new to Node Js and Webpack. I tried to start a project with module-loaders.
Firstly, I installed nodeJs and NPM and created a new directory called tutorial. I used the command prompt to cd into this directory and then ran the following command npm init and then installed webpack via npm using the command below :
npm install -S webpack
The 1st command installed webpack locally into the project under the 'node-modules' directory and I can run my project by doing this:
nodejs node-modules/webpack/bin/webpack.js
The problem with this is that I have to place my webpack.config.js file inside of this directory which I want to place in my project root.
One solution to this problem was to install webpack globally on my machine which I did using the command below :
npm install -g webpack
This installed Webpack and now I do have a Webpack command. However, this command does not seem to be working or doing anything at all. When I try to run this from my project's root directroy it does not do anything at all (See Screenshot)
Please tell me what I am doing wrong!!
webpack is not only in your node-modules/webpack/bin/ directory, it's also linked in node_modules/.bin.
You have the npm bin command to get the folder where npm will install executables.
You can use the scripts property of your package.json to use webpack from this directory which will be exported.
"scripts": {
"scriptName": "webpack --config etc..."
}
For example:
"scripts": {
"build": "webpack --config webpack.config.js"
}
You can then run it with:
npm run build
Or even with arguments:
npm run build -- <args>
This allow you to have you webpack.config.js in the root folder of your project without having webpack globally installed or having your webpack configuration in the node_modules folder.
You can run npx webpack. The npx command, which ships with Node 8.2/npm 5.2.0 or higher, runs the webpack binary (./node_modules/.bin/webpack) of the webpack package.
Source of info: https://webpack.js.org/guides/getting-started/
I had to reinstall webpack to get it working with my local version of webpack, e.g:
$ npm uninstall webpack
$ npm i -D webpack
npm i webpack -g
installs webpack globally on your system, that makes it available in terminal window.
The problem with my setup was webpack was installed but webpack-cli was missing
npm i -g webpack webpack-cli
If you prefer to install locally then install without -g flag
The quickest way, just to get this working is to use the web pack from another location, this will stop you having to install it globally or if npm run webpack fails.
When you install webpack with npm it goes inside the "node_modules\.bin" folder of your project.
in command prompt (as administrator)
go to the location of the project where your webpack.config.js is located.
in command prompt write the following
"C:\Users\..\ProjectName\node_modules\.bin\webpack" --config webpack.config.vendor.js
Installing webpack with -g option installs webpack in a folder in
C:\Users\<.profileusername.>\AppData\Roaming\npm\node_modules
same with webpack-cli and webpack-dev-server
Outside the global node_modules a link is created for webpack to be run from commandline
C:\Users\<.profileusername.>\AppData\Roaming\npm
to make this work locally, I did the following
renamed the webpack folder in global node_modules to _old
installed webpack locally within project
edited the command link webpack.cmd and pointed the webpack.js to look into my local node_modules folder within my application
Problem with this approach is you'd have to maintain links for each project you have. Theres no other way since you are using the command line editor to run webpack command when installing with a -g option.
So if you had proj1, proj2 and proj3 all with their local node_modules and local webpack installed( not using -g when installing), then you'd have to create non-generic link names instead of just webpack.
example here would be to create webpack_proj1.cmd, webpack_proj2.cmd and webpack_proj3.cmd
and in each cmd follow point 2 and 3 above
PS: dont forget to update your package.json with these changes or else you'll get errors as it won't find webpack command
Actually, I have got this error a while ago. There are two ways to make this to work, as per my knowledge.
Server wont update the changes made in the index.js because of some webpack bugs. So, restart your server.
Updating your node.js will be helpful to avoid such problems.

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