I need to run two servers (www.js and apiServer.js) simultaneously using nodemon. I have specified that value for the "start" key in package.json as
When I run: nodemon in the command prompt with current working directory in which package.json is placed, only "apiServer.js" is running. The other "www.js" is not running.
I would appreciate if somebody could help with this problem. Thanks in advance.
The && operator will run processes sequentially, so the second process doesn't start because the first process doesn't resolve.
Try the concurrently NPM module - it allows you to run simultaneous processes - https://www.npmjs.com/package/concurrently
So after you install it, your command would look like
"start": "concurrently \"node apiServer.js\" \"node ./bin/www.js\""
Related
I've installed cypress framework on my project.
I already have multiple viewports tests running successively on my app.
Is it possible to automate cross browser testing, so I don't have to select a browser extension to run my test on?
Thanks in advance! :-)
Yes, it is possible. In this case, there can be scenarios that you can run concurrently and there can be scenarios you need to run sequentially. I'll add the solution which I found for both problems.
For this, we need the package.json file. ( If you don't have it do an npm init ) and it will guide you through making the pachage.json
Then let's say we have two test files 1 can be run concurrently, and the other one should not be executed concurrently. let's name them as concurrently.js and sequentially.js
To run the tests sequentially ad your cypress command inside the scripts JSON object.
Ex:
"scripts": {
"cy:sequence:chrome": "cypress run --browser chrome --spec **/sequentially.js",
"cy:sequence:firefox": "cypress run --browser firefox --spec **/sequentially.js",
}
Now you just need to run npm run command to sequence execution, in this case, it will be
npm run cy:sequence:chrome && npm run cy:sequence:firefox
Then if you need concurrent execution, we need to add an npm package [concurently][1]
You can install it with npm i concurrently
Now like we did before we need to add the concurrently.js execution commands to scripts. Here we are using the concurrently package notations to add it. ( check the above URL )
so our final scripts package looks like,
"scripts": {
"cy.concurent:run" : "concurrently \"cypress run --browser chrome --spec **/concurrently.js\" \"cypress run --browser firefox --spec **/concurrently.js\"",
"cy:sequence:chrome": "cypress run --browser chrome --spec **/sequentially.js",
"cy:sequence:firefox": "cypress run --browser firefox --spec **/sequentially.js",
}
if the above addition is not clear, we need to add the commands as quotes with concurrently word being the first word. it should be like
"concurrently "command1" "command2" "
Now if you need to run the tests concurrently in multiple browsers, you just need to run
npm run cy.concurent:run
My only doubt is about the reporting, you might need to add an additional config to save browser-specific videos and reports. If not it might save the data of last execution.
Hope this helps,
Cheers.
[1]: https://www.npmjs.com/package/concurrently
I am new to React and attempting to use npm test.
From the docs, it is my understanding that every time npm test runs, it starts the watcher. The watcher is supposed to display a list of commands titled "Watch Usage". This includes commands such as a, f and q.
When I run npm test, watcher appears to work because it runs the tests and reruns them if I save a file.
However, it does not display the "Watch Usage" list and if I type any of the "Watch Usage" commands, nothing happens.
How can I enable the "Watch Usage" commands?
I was attempting to run this command in Git Bash. I have not found a solution to make it work in Git Bash.
However, you can instead run the command in cmd.exe and it will work properly.
The Jest VSCode extension can also be helpful.
There is a problem with running Jest inside Git Bash on Windows. The terminal codes for updating the screen are not correctly handled, and I've been unable to find a reason or fix for this searching Google.
If you run npm test from a DOS prompt, then it seems to work fine, but if you run npm test from a Git Bash prompt the progress feedback from Jest is missing.
You can enable colors by running npm run test -- --colors which gets Jest to output the color status for tests in Git Bash, but strangely the colors are working but the progress is still not shown.
The only successful work around that I've found is running Jest from inside winpty if you're using Git Bash.
winpty npm.cmd test
So what I do is add an alternative npm script to my package.json file for running on Git Bash inside Windows.
"wintest": "winpty npm.cmd run test",
Then you can just run that instead.
npm run wintest
It's not a perfect solution, but at least you can see the progress correctly.
You can activate watch mode, modify your package.json:
"scripts": {
"test": "jest --watch",
}
run npm test
I'm just starting to learn about how JavaScript, HTML, and Electron all work, and I want to know what runs electron . in the "scripts" -> "start" of package.json, because I can't tell what does and that kind of wizardry makes me nervous.
According to the man pages for npm, what npm start does is that it reads the package.json, looks at the script under "scripts" -> "start" -> some_script, and then runs some_script. Sometimes, some_script is something like node foobar.js, which makes sense to me, since I can run that from the command line. NodeJS is executing foobar.js. However, in the case of the electron-api-demos, some_script is electron .
You can download and run electron-api-demos via
git clone https://github.com/electron/electron-api-demos
cd electron-api-demos/
npm install && npm start
In order to try to figure out what is running electron ., I've run it in the node shell, and I've tried running node main.js. I've even tried opening up the node shell and running
electron-api-demos#2.0.2 start $DIR/electron-api-demos
electron .
(which is exactly the output of npm start). None of them worked, because none of them started up the Electron application. At this point I'm very puzzled at how, exactly, the start script gets executed at all.
So I guess my question is: does there exist a command (that I can use on the command line) to start up this Electron application, without using npm? If not, what is npm calling to start up this Electron app?
I apologize if this question has been asked before, but I all the sources I found didn't seem to go into any further detail about what, exactly, is done when npm start is run and how it executes electron . . Thank you for your time!
Command line interfaces installed with npm are put in the node_modules/.bin/ directory. You can't just run them from the command line because that directory isn't in your PATH (unless you put it there, or you installed it globally).
So, if you want to run electron without npm start, you can run ./node_modules/.bin/electron .. Since this is a bit verbose, newer versions of npm provide the command npx to run things without the ./node_modules/.bin/ part, so npx electron . also works.
Since npm scripts often use the packages you've installed, they automatically add node_modules/.bin/ to the PATH before running your command. As a result, the start script can just reference electron directly.
npx can do some other cool things too – npm has a blog post about it.
When you run npm start , it by default run command corresponding "start" key of script property of package.json like
"script":{
"start": "ng serve",
"launch":"electron main.js" or "electron ." // main.js located in the same dir
"test": " ng test"
}
same when you run npm run launch it will trigger the command corresponding of the "launch" key of script property of package.json file. like run electron main.js command and your application will launched.
so if you want to run the your electron application directly like electron main.js then install the electron module globally using command npm install electron -g then simply run the electron main.js command and your application will start.
I think the title is self explain but again:
what is the benefit of using the pre script of npm packege.json for example prestart over just concatenate commands with && in the start script?
{
prestart: "parcel build",
start "nodemon server.js"
}
vs
{
start: "parcel build && nodemon server.js"
}
It is more cross platform ?
can it handle two async endless process like two servers (build + api) ?
something else?
edit:
I found benefit for postInstall. Heroku and such delete devDependency after npm install so in postinstall I can put build process before Heroku delete the code that do that.
prestart runs before start as the name suggests, therefore running a command in prestart and a command in start runs the two commands in sequence, not parallel. Running commands in start with && runs them sequentially, but inside the same step.
The two methods are pretty much the same, at least in terms of results. However, there might be compatibility issues with && on certain versions of Windows.
If you want to run commands in parallel, you can use & inside start, instead of &&.
In addition to the other answers, it should be noted that the prestart hook in package.json is not supported by the yarn package manager. So in this respect, using && allows for easier migration between npm and yarn (provided that your shell can interpret the &&).
The reason for this is to increase maintainability by avoiding implicit dependency chains:
In particular, we intentionally don't support arbitrary pre and post hooks for user-defined scripts (such as prestart). This behavior, inherited from npm, caused scripts to be implicit rather than explicit, obfuscating the execution flow. It also led to surprising executions with yarn serve also running yarn preserve.
https://yarnpkg.com/advanced/lifecycle-scripts
These methods are more for clarity in code, for separation of logical steps.
About compatability. As I understand npm runs all scripts in the local shell, so on most linux systems it will be some sh clone, and on windows it will be cmd. So there may be situation where && will not be supported by the shell. But it's unlikely and do you really need to support such behaviour, considering users could install bash on any platform node.js could be installed on and set nom to use it? I personally use bash in npm scripts and document in the README.
If you want to run multiple long-running processes use something like pm2 https://github.com/Unitech/PM2/ in production. When you're developing, usually it's helpful to run processes in multiple terminals to see logs, use supervisor https://github.com/petruisfan/node-supervisor to restart processes on errors and changes.
Also I usually write .sh scripts for maintenance, like deploy and periodic, but manual tasks and run them using npm - you could add any named scripts in scripts section.
That is because when you run a script like this - npm install && npm start node will run both commands in the same process, so if one of the commands will send any non-zero exit code (in case of an exception) you'll not be able to determine which command failed. Running them in separate scripts with "pre" keyword will execute them in separate processes, so it is more accurate.
Is it possible to append to a task to "npm install". I have a node project in which I'm using Grunt as task runner. To fully install the project, one of course needs to get the dependencies by running "npm install". After this it is still not fully setup however, and one needs to also run a command via grunt. Now I'd like all required installation tasks to be done with a single command.
Is it possible to append a task to run to "npm install", so it becomes "npm install ; grunt mytask" when someone runs "npm install"?
Your package.json file can contain a "scripts" property: https://npmjs.org/doc/misc/npm-scripts.html
The "postinstall" script sounds like it would fit your needs.
I re-read your post, and I think I understood it wrong the first time.
I believe what you are looking for is here: https://npmjs.org/doc/scripts.html
Hth,
Aaron