Node & Yarn Command Means - node.js

I am trying to found
npm run install:global
npm install -g win-node-env
yarn && npm run dev:build && npm run dev:serve
What does this three command means ?
I tried to search google but not got relative info.

Without any extra info...
1:
npm run runs the next argument defined in the "scripts" property of your package.json file. So, npm run install:global will execute whatever the corresponding script entry by that name. If your package.json file has this entry (and it should):
package.json:
"scripts": {
"install:global": "npm install -g whatever-the-script-is"
}
2:
npm install -g win-node-env will globally install the win-node-env package, so that it's accessible from a terminal session by typing whatever win-node-env exposes.
3:
yarn && npm run dev:build && npm run dev:serve
Yarn is an alternative for npm. This command would run yarn, then the script in package.json that correspond to dev:build and immediately after run dev:serve.

npm run install:global
Run specified npm script
npm install -g win-node-env
Install package globally
yarn && npm run dev:build && npm run dev:serve
same as
npm install && npm run dev:build && npm run dev:serve
Install local packages and run script
This is how package.json might look like
{
"name": "demo"
"scripts": {
"install:global": "npm install -g my-unrealistic-package"
"dev:build": "echo \"build something\"",
"dev:serve": "echo \"serve something\""
}
}

Related

Why can npm run babel-node from script but not as npm exec?

In a lerna monorepo, babel is installed only at the root package level.
When running npm exec babel-node from root/packages/packageA, it fails with:
npm ERR! could not determine executable to run
From the same directory, running npm run test, where test is just set to babel-node opens the babel-cli.
I.e. why can npm resolve the path to the root node_modules' bin when run as a script but cannot when run from the terminal directly?
EDIT:
I.e. https://github.com/bishonen/newproject/tree/master/packages/someapp
when running npm run test-babel, it will work from any of the 3 directories which contain a package.json.
When running npm exec babel-node, it will only work from the root directory.
Workspaces support for both npm run and npm exec would be supported in npm#7.7.0 or greater. See the details here
You can use the feature updating npm
npm i -g npm#7
I try it and it works in your code
https://github.com/bishonen/newproject/tree/master/packages/someapp
I use npm v7.12.0

Installing fresh react app with dockerfile

I'm trying to run a fresh installation of React using a Dockerfile.
FROM node:12.14-alpine
RUN mkdir /app
WORKDIR /app
RUN npm install -g nodemon \
npx create-react-app .
COPY package.json package.json
RUN npm install --silent
COPY . .
CMD [ "node", "start" ]
My root folder is empty so no package.json file exists etc. When I run docker-compose up --build I get the following error which indicates that's failing on npx create-react-app .
Step 5/9 : RUN npm install -g nodemon npx create-react-app .
---> Running in c4878af5f94d
npm ERR! code ENOLOCAL
npm ERR! Could not install from "" as it does not contain a package.json file.
npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2020-02-18T12_55_14_899Z-debug.log
ERROR: Service 'react' failed to build: The command '/bin/sh -c npm install -g nodemon npx create-react-app .' returned a non-zero code: 1
Any ideas what I'm doing wrong? I'm not having node or npm installed nativelly so all needs t run through docker
You need to delete the . at the end of the RUN :
RUN npm install -g nodemon \
npx \
create-react-app
You forgot "&&" at the beginning line 7
FROM node:12.14-alpine
RUN mkdir /app
WORKDIR /app
RUN npm install -g nodemon \
&& npx create-react-app .
COPY package.json package.json
RUN npm install --silent
...
CMD [ "node", "start" ]

Script for initial build of Angular 2 project?

How can I write a script for npm that installs the node_modules in an angular 2 project and compiles the ts files. This doesn't work:
"scripts": {
"firstBuild": "npm install && tsc",
....
I get the error:
> npm firstBuild
Usage: npm <command>
where <command> is one of:
PS:
I can run "start" : "tsc && concurrently \"tsc -w\" \"lite-server\" just like npm start , why can't I do npm firstBuild ?
I think you just forgot to use run.
Try npm run firstBuild.

How to specify the path of `package.json` to npm?

I use npm scripts to build my project. I'd like to be able to run the scripts from a different directory. That is, instead of doing the following:
cd project;
npm run build;
cd ..
...I'd like to simply do something like:
npm run build -config project/package.json;
or
npm run build -wd project;
Is this possible?
Using --prefix worked for me:
npm --prefix /path/to/project run build
Where path/to/project is the directory where your package.json with build command defined.
to change the path of package.json that npm gets
this didnt work for me
npm --prefix /path/to/project run build
but this did
npm --prefix /path/to/project run
but this does not permanently change it
for example you can say
npm --prefix /path/to/project run test

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