who does npm install of webpack-dev-server require '-g'? - node.js

Trying to run "webpack-dev-server --open" from VSCode bash terminal. Error: webpack-dev-server not found
Why do I have to do the install with '-g'? If I install local, I get the not found error. In this case, do I have to modify the path to pick up the local install?
npm install webpack-dev-server -g

When you install webpack-dev-server locally in your project, you can run it with npx:
npx webpack-dev-server --open
Or:
./node_modules/.bin/webpack-dev-server --open
You can also add a script in package.json:
scripts: {
dev: "webpack-dev-server --open"
}
Then run npm run dev.
There's no need to install it globally, which is not recommended.

Related

Getting "Cannot find module '/app/dist'" error on deploying to Heroku

I have the following script in my package.json:
"scripts": {
"dev": "nodemon server --exec babel-node --presets es2015,stage-2",
"build": "babel ./server -d ./dist",
"start": "node ./dist",
"heroku-postbuild": "cd react-ui/ && npm install && npm install --only=dev --no-shrinkwrap && npm run build"
},
On deploying to Heroku I get the following error
Error: Cannot find module '/app/dist'
On local npm run dev, npm run build, and npm run start work fine.
Where is it getting the /app folder? How to fix this?
Thanks
Matloob
You should build your app npm run build before deploy to heroku. It will run heroku-postbuild first then start your app.
Try this :
"start:heroku": "node dist/YOUR-APP-NAME/server/main.js",

npm install not working with --prefix

It seems that npm install --prefix ./server (with no args) is not working with --prefix flag. I just want to install all packages from package.json.
All I get after that command is:
npm WARN enoent ENOENT: no such file or directory, open
'/home/.../ProjectName/server/package.json'
All is fine when I put npm install package_name -S --prefix ./server for example. Then NPM will create node_modules in server and will add package_name package.
My files structure is:
ProjectName
|
+-- client
| +-- node_modules
| +-- package.json
+-- server
| +-- node_modules
+-- package.json
"Main" package.json contains all scripts (for Heroku and for me) and dependiencies for server.
client is Angular2 app that's why it has own node_modules and package.json.
I use NPM 4.2.0. With version 5.0.3 (newest?) it seems that --prefix flag is not working at all.
EDIT #1
I've just discovered that I can solve my problem with npm install (which will install node_modules in my project folder) and then copy node_modules to server/node_modules. Without that copy jasmine throws errors during tsc build.
Now I have to have node_modules in main catalog and copy of them in server. That's so odd..
EDIT #2
According to #Damian Lattenero answer:
npm --prefix ./server install ./ProjectName/package.json
or
npm --prefix ProjectName/server install ./ProjectName/package.json
IS NOT WORKING and generates:
npm ERR! code ENOLOCAL npm ERR! Could not install
"RecursiveNotebook3/package.json" as it is not a directory and is not
a file with a name ending in .tgz, .tar.gz or .tar
THIS WORKS:
npm --prefix ProjectName/server install ./ProjectName
but generates:
npm WARN saveError ENOENT: no such file or directory, open
'/home/tb/Projects/RecursiveNotebook3/server/package.json' npm notice
created a lockfile as package-lock.json. You should commit this file.
npm WARN enoent ENOENT: no such file or directory, open '/home/
tb/Projects/RecursiveNotebook3/server/package.json'
and
package-lock.json next to node_modules
and
empty etc catalog next to node_modules
and
There are some problems with build (tsc -p server) with mongodb package.
Try:
npm --prefix ./server install ./ProjectName/package.json
or
npm install --prefix ./server ./ProjectName/package.json
Also, to understand better what the --prefix do, you can check this two answers:
How to npm install to a specified directory?
npm - install dependencies for a package in a different folder?
Works for me
npm install --prefix ./server ./server
Running the newest version of Ubuntu (Ubuntu 16.04.2 LTS), I encountered the same problem with npm install. I also got an ENOENT error, indicating that npm cannot find the necessary files.
When I installed nodejs-legacy, as shown here under:
sudo apt-get install nodejs-legacy
npm subsequently compiled fine, and my Angular application deployed as it should.
SOLUTION
Those lines in package.json solves all my problems:
"scripts": {
"init": "npm i && mv ./node_modules ./server && sudo npm i typescript -g",
Strange but works...
This part of my server package.json and all working fine:
"scripts": {
"start": "node dist/app.js",
"server": "nodemon --exec ts-node src/app.ts",
"build": "tsc -p .",
"client": "npm start --prefix ../client",
"client:install": "npm install --prefix ../client",
"client:build": "npm run build --prefix ../client",
"dev": "concurrently \"npm run server\" \"npm run client\""
},
Try this one it will definitely work, I'm assuming your project root directory package.json also has dependencies.
npm install && npm install --prefix ./server && npm install --prefix ./client
or user this script
"scripts": {
"client-install": "npm install --prefix ./client",
"install-all": "npm install && npm run client-install && npm run server-install",
"server-install": "npm install --prefix ./server",
},

How to run start scripts from package.json?

I've two scripts in package.json
"start:dev": "nodemon ./src/index.js",
"start": "npm run build && node ./build/index.js",
npm start works well.
I need to run "start:dev": "nodemon ./src/index.js"
For most custom npm scripts you need to add run before script name
npm run start:dev
npm - The main scripts such as start, stop, restart, install, version or test do not require run command. These scripts and some other are described in npm documentation.
npm start
The others need run command before the script name as was pointed by David.
npm run start:dev
Yarn - You can also use yarn and in that case you do not have to specify run.
yarn start
yarn start:dev

Unsupported platform for inotify#1.4.1: wanted {"os":"linux","arch":"any"}

I am coming across a very bizarre error when installing packages for my webpack/reactjs application.
I am currently trying to install
npm install copy-webpack-plugin --save dev
The build/start scripts fail and the following error is shown in the terminal:
Unsupported platform for inotify#1.4.1: wanted {"os":"linux","arch":"any"} (current: {"os":"darwin","arch":"x64"})
This means absolutely noting to me and I cannot find any clear explanations online.I do not know why inotify is needed or when/where it has been installed.
Package.json scripts:
"scripts": {
"start": "node server.js",
"build": "cross-env BABEL_ENV=production ./node_modules/.bin/webpack --config webpack.config.production.js",
"lint": "eslint --cache --ignore-path .gitignore --format=node_modules/eslint-formatter-pretty . *.js",
"test": "npm run lint"
},
There is a typo in your command:
npm install copy-webpack-plugin --save dev
Should be:
npm install copy-webpack-plugin --save-dev
(You forgot a dash)
The typo will make npm try to install https://www.npmjs.com/package/dev, which depends on inotify.
Your error message is because inotify only works on Linux and you are on a Mac.
You unintentionally tried to install dev package https://www.npmjs.com/package/dev that is not supported on your OS.

nodemon not working: -bash: nodemon: command not found

I'm on a Mac running El Capitan. I have node v5.6.0 and npm v3.6.0.
When I try to run nodemon, I get:
-bash: nodemon: command not found
I thought this may mean that I didn't have nodemon installed, so when I tried to install it using...
sudo npm install -g nodemon
...I get this:
npm ERR! Darwin 15.2.0
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "install" "-g" "nodemon"
npm ERR! node v5.6.0
npm ERR! npm v3.6.0
npm ERR! path /usr/local/bin/nodemon
npm ERR! code EEXIST
npm ERR! Refusing to delete /usr/local/bin/nodemon: ../lib/node_modules/nodemon/nodemon.js symlink target is not controlled by npm /usr/local
npm ERR! File exists: /usr/local/bin/nodemon
npm ERR! Move it away, and try again.
npm ERR! Please include the following file with any support request:
npm ERR! /Users/brianeoneill/npm-debug.log
If it makes a difference, I'm trying to run nodemon on a project that uses Express v4.13.1
Thanks for any help you can offer!
I tried the following, and none worked:
npm uninstall nodemon
sudo npm uninstall -g nodemon
What did work was:
sudo npm install -g --force nodemon
If you want to run it locally instead of globally, you can run it from your node_modules:
npx nodemon
From you own project.
npx nodemon [your-app.js]
With a local installation, nodemon will not be available in your system path. Instead, the local installation of nodemon can be run by calling it from within an npm script (such as npm start) or using npx nodemon.
OR
Create a simple symbolik link
ln -s /Users/YourUsername/.npm-global/bin/nodemon /usr/local/bin
ln -s [from: where is you install 'nodemon'] [to: folder where are general module for node]
node : v12.1.0
npm : 6.9.0
I'm using macOS/Linux, the solution that works for me is
npx nodemon index.js
I have tried every possibility, like uninstalling and installing nodemon, installing nodemon globally. restart the terminal, but it won't work.
don't try such things to waste your time.
in Windows OS run:
npx nodemon server.js
or add in package.json config:
...
"scripts": {
"dev": "npx nodemon server.js"
},
...
then run:
npm run dev
I had the same exact problem, expect for Windows OS.
For me, running
npm install -g nodemon --save-dev
(note the -g) worked.
Maybe somebody else who has this problem on Windows will have the same solution.
FOR WINDOW USERS
I tried every possible way but nothing worked for me.
What worked was:-
npx nodemon server
FOLLOWING WILL BE THE OUTPUT:-
For mac Users, use npx nodemon index.js
...
"scripts": {
"start": "npx nodemon index.js"
},
...
> my-project#1.0.0 start
> npx nodemon index.js
Need to install the following packages:
nodemon
Ok to proceed? (y)
[nodemon] 2.0.13
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node index.js`
Server Started on Port 8000
sudo npm install nodemon --save-dev
Next package.json on and
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon app.js"
}
Type on terminal (command prompt)
npm start
Install nodemon:
sudo npm install -g nodemon
Run server:
sudo nodemon server.js
I ran into the same problem since I had changed my global path of npm packages before.
Here's how I fixed it :
When I installed nodemon using : npm install nodemon -g --save , my path for the global npm packages was not present in the PATH variable .
If you just add it to the $PATH variable it will get fixed.
Edit the ~/.bashrc file in your home folder and add this line :-
export PATH=$PATH:~/npm
Here "npm" is the path to my global npm packages . Replace it with the global path in your system
In macOS,
I fixed this error by installing nodemon globally
npm install -g nodemon --save-dev
and by adding the npm path to the bash_profile file. First, open bash_profile in nano by using the following command,
nano ~/.bash_profile
Second, add the following two lines to the bash_profile file (I use comments
"##" which makes it bash_profile more readable)
## npm
export PATH=$PATH:~/npm
For nodemon: not found command
I tried with many links but was not working then i tried with the below steps it worked fine.
Follow this step it worked for me.
step1 : sudo su
step2 : npm install -g nodemon --save-dev
It is working fine.
Just in case for those using Windows, you don't need sudo
npm i -g nodemon
sudo su
then
npm install nodemon
worked for me
Just writing what did worked for me - (on Windows machine, installing node locally to the project)
if you do not want to install it globally (i.e without -g flag) you have to use
npx nodemon app
where app is your app.js is your program file to launch.
Make sure you own root directory for npm so you don't get any errors when you install global packages without using sudo.
procedures:-
in root directory
sudo chown -R yourUsername /usr/local/lib/node_modules
sudo chown -R yourUsername /usr/local/bin/
sudo chown -R yourUsername /usr/local/share/
So now with
npm i npm -g
you get no errors and no use of sudo here.
but if you still get errors confirm node_modules is owned again
/usr/local/lib/
and make sure you own everything
ls -la
now
npm i -g nodemon
will work!
Since Node v18.11.0 there is running in 'watch' mode using
node --watch
which restarts the process when an imported file is changed.
https://nodejs.org/en/blog/release/v18.11.0/
Following commands worked for me in my case
Open Windows Powershell and Run series of following Commands,
Get-ExecutionPolicy -List
Set-ExecutionPolicy Unrestricted
*Press Y for YES*
Set-ExecutionPolicy Unrestricted -Force
Here you Go.
In Windows git bash, I fixed it by restarting git bash
Put --exec arg in single quotation.
e.g. I changed "nodemon --exec yarn build-langs" to "nodemon --exec 'yarn build-langs'" and worked.

Resources