How do you correctly use parallelshell with npm scripts? - node.js

I am trying to use parallelshell with my node project on Windows to run two processes at the same time.
Here is the scripts section of my package.json file:
"scripts": {
"start": "npm run watch:all",
"test": "echo \"Error: no test specified\" && exit 1",
"lite": "lite-server",
"scss": "node-sass -o css/ css/",
"watch:scss": "onchange \"css/*.scss\" -- npm run scss",
"watch:all": "parallelshell \"npm run watch:scss\" \"npm run lite\""
}
When I run the command npm start I get this error log:
TypeError [ERR_INVALID_ARG_TYPE]: The "options.cwd" property must be of type string. Received type function
at normalizeSpawnArguments (child_process.js:420:11)
at spawn (child_process.js:522:38)
at C:\Users\Daniel\Documents\development\online_classes\coursera_uhk_web_dev\Bootstrap4\conFusion\node_modules\parallelshell\index.js:104:17
at Array.forEach (<anonymous>)
at Object.<anonymous> (C:\Users\Daniel\Documents\development\online_classes\coursera_uhk_web_dev\Bootstrap4\conFusion\node_modules\parallelshell\index.js:100:6)
at Module._compile (internal/modules/cjs/loader.js:702:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
at Module.load (internal/modules/cjs/loader.js:612:32)
at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
at Function.Module._load (internal/modules/cjs/loader.js:543:3)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! confusion#1.0.0 watch:all: `parallelshell "npm run watch:scss" "npm run lite"`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the confusion#1.0.0 watch:all script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
Is there something wrong with my syntax? I can run the commands npm run watch:scss and npm run lite individually and they work fine, but I am not able to run the parallelshell command.
Thank you!

Try downgrading the parallelshell version from 3.0.2 to 3.0.1
According to the statement, it is just a temporary fix.
https://github.com/darkguy2008/parallelshell/issues/57
Below is the command line syntax to for downgrading parallelshell:
sudo npm uninstall --save-dev parallelshell#3.0.2
sudo npm install --save-dev parallelshell#3.0.1
It worked for me.
Hope this helps and let us know.

parallelshell is giving active errors at every use. Instead of parallelshell the alternative here is to use npm-run-all dev-dependency.
install npm-run-all
npm install --save-dev npm-run-all
then make active changes in scripts of package.json shown below.
"scripts": {
"start": "npm run dev",
"test": "echo \"Error: no test specified\" && exit 1",
"lite": "lite-server",
"scss": "node-sass -o css/ css/",
"watch:scss": "onchange \"css/*.scss\" -- npm run scss",
"dev": "npm-run-all -p watch:scss lite"
}

All correct to be run on Windows OS. Just simply try to install parallelshell#3.0.1:
npm install --save-dev parallelshell#3.0.1
and then call npm start again.

change a line in your node_modules/parallelshell/index.js:105 file
from: cwd: process.versions.node < '8.0.0' ? process.cwd : process.cwd(),
to: cwd: parseInt(process.versions.node) < 8 ? process.cwd : process.cwd(),
reference: Problem running parallelshell Nodejs script

This is due to incompatibility of the npm with the version of parallelshell.
Try downgrading it to 3.0.1 by using:
npm i --save-dev parallelshell#3.0.1
Worked for me 100%.

It looks correct to me, instead of escaping your double quotes you could use single quotes inside the double quotes.
Not sure if it will make a difference.
"watch:all": "parallelshell 'npm run watch:scss' 'npm run lite'"

Is different for MAC and Windows machines.
User single quotes on MAC and "\ "\ on windows.
Also downgrade to parallelshell to 3.0.1 to work with \"

I changed to use concurrently package, and it works well for me on win64, the method mentioned is in the post.
I just simply use "watch:all": "concurrently \"npm run watch:scss\" \"npm run lite\"" to replace "watch:all": "parallelshell \"npm run watch:scss\" \"npm run lite\""

This is a common issue can not be fixed using npm audit fix. All you can do is to copy the actual index.js file of parallelshell into your node_modules directory.
So to do it below are the instructions:
1. Go to https://raw.githubusercontent.com/darkguy2008/parallelshell/master/index.js
2. Copy the content here.
3. Now go to the directory of your project, it may be something like /project/node_modules/parallelshell/index.js
4. Inside the index.js replace the contents with the one you copied from the link in Step 1.
5. Save the file and exit.
Hope this fix will work for you.

If someone is still running into errors after altering the index.js or after demoting to 3.0.1 version, a better alternative would be using npm-run-all

Related

Npm start and npm run build, why to use `run` when build? [duplicate]

I have checked both commands npm start and npm run start, both works perfectly. I used create-react-app. But to make configuration changes in the CSS module, I run npm eject but it throws an error.
But npm run eject worked? I'm confused on why npm eject didn't work. Can I configure this?
Below is my package.json:
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
}
npm test, npm start, npm restart, and npm stop are all aliases for npm run xxx.
For all other scripts you define, you need to use the npm run xxx syntax.
See the docs at https://docs.npmjs.com/cli/run-script for more information.
npm start is the short form for npm run start. So, its one and the same thing.
One interesting thing to note is,
If the scripts object does not have a "start" property in package.json file, npm start or npm run start from command line will run node server.js by default.
But if the scripts object in package.json has "start" property, it overrides node server.js and executes the command in "start" property.
See - https://docs.npmjs.com/cli/v7/commands/npm-start#description
Telling this because sometimes this can be help to someone.
Inside the jenkins pipelines you should use npm commands with "run"
ex- npm run start
if not the command will not be executed.

How do I get SASS with auto-refresh?

When entering the command
npm install sass --watch ...
I get back,
npm ERR! enoent This is realated to npm not being able to find a file.
Though, the file is there and everything is spelled correctly.
Can anyone help?
Hey John take a look at this reply. Basically there are different solutions:
Single Ampersand solution
Adding to your package.json the following:
"dev:watch" : "npm run sass:watch & npm run livereload"
Parallelshell solution
Using Parallelshell and adding to your package.json the following:
"serve": "live-server",
"start": "parallelshell \"npm run scss && npm run scss -- -w\" \"npm run serve\""
Concurrently solution
Using Concurrently. Install it npm install concurrently --save-dev and add the script:
"dev:watch": "concurrently \" npm run sass:watch \" \" npm run livereload \" "

npm ERR! missing script: deploy

I am deploying my application to github pages but when I run command npm run deploy but i get error
npm ERR! missing script: deploy
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\whoami\AppData\Roaming\npm-cache\_logs\2020-02-05T16_46_06_946Z-debug.log
I have defined deploy in my package.json file
...
"scripts": {
"test": "jest --watchAll --verbose --coverage --runInBand",
"start": "node index.js",
"predeploy":"npm run build",
"deploy":"gh-pages -d build"
}
...
make sure you have save package.json file. In my case i have not saved it so that is why error is coming
Please check the terminal path. In my case, I forgot to change the directory inside my project.
Simply type the following:
cd *name-of-your-project*
Please close the current working terminal and open new terminal and try the command again.

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

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.

Resources