Run two scripts simultaneously - javascript

I created a web application in azure to host my application js node. (Azure web application)
In my project I have an api in express that is in the app.js, however in the same project I have another file which is a cronjob.
In my package.json I have the following configuration for script:
"scripts": {
"start": "node app.js"
}
When deploying through github, the api that is in the app.js works perfectly.
My question: How do I run cronjob.js simultaneously with app.js?

You can start multiple application by using "pm2" node_module.
After installing pm2 module you can start your application by using the following command in terminal.
pm2 start app.js && pm2 start cronjob.js
You may also use forever node module.

Another option to running multiple scripts simultaneously is npm-run-all.
Install with:
npm install --save npm-run-all
Then setup your "scripts" section in your package.json like so:
"scripts": {
"app": "node app.js",
"cronjob": "node cronjob.js",
"start": "npm-run-all --parallel app cronjob"
}
And start with npm start as usual.

If the only requirement is that, I think there is no need to use another tool. Simply, you can achieve this with a single ampersand &.
"scripts": {
"start": "node app.js & node cronjob.js"
}

Related

Trouble with running a react + next.js project

I'd like to fork this repository and run it on my local server:
https://github.com/carbon-app/carbon.git
I am familiar with React but new to Next.js. The scripts in the package.json are specified as follows:
"scripts": {
"dev": "next",
"build": "next build",
"start": "next start",
How can I run this as a React project using npm? I'm interested in the React part to recreate another app using this app's frontend.
Please help me to run as a react project.
first, you should install dependencies:
npm i
then run next js in development mode using:
npm run dev
for more details, visit Next.js docs

Can my NPM CLI package be executed on CMD without installing globally?

I have written an NPM package which has its own CLI commands.
Let's name that package as xyz and imagine it's now avaialable on npmjs.com
So, let's say a user installs this package in his project by running npm install xyz.
And now he wants to run a CLI command provided by xyz package on his terminal in his project.
xyz do_this
Can this be done without installing this package globally by user ? Or without any further configuration for the user ?
Here's some part of the package.json of the xyz package.
{
"name": "xyz",
"version": "1.0.0",
"description": "Description",
"main": "index.js",
"preferGlobal": true,
"bin": "./index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
........
Here's how npm works. When you install a package in the local directory, it installs the executable dependencies files inside node_modules inside the folder with package.json, but if you use --global, it places it globally where the user has set their path.
For example, when I run npm install http-server, my executable ends up as ./node_modules/http-server/bin/http-server but when i install it globally, I have it as node_modules/http-server/bin
The work around for this is, if you want to just run the executable, just execute it inside like so ./node_modules/http-server/bin/http-server. If you want it as a command, you'll need too have the user add the directory to their Path so when the user enters the command, the computer looks for the command inside that folder.
Here's a guide to adding PATH directories in Linux, you'll just add the directory /pathtofolder/node_modules/http-server/bin/ or whatever your folder is. https://linuxize.com/post/how-to-add-directory-to-path-in-linux/
For example, if I wanted to add http-server from my local folder to my path, I would run
export PATH="/pathtofolder/node_modules/http-server/bin/:$PATH"
Good luck! Let me know how I can help you!

How to Build NextJS Project on Local without Now

I need to build My React NextJS Project on local to host it at Appache server, when I run command run build it did not generate build folder.
I R & D on it, everyone recommend ZEIT – Next.js build with Now but it build project on cloud but I need a build for my local appache server. So please help me out.
Here is an my of package.json:
......
"scripts": {
"dev": "node server.js",
"build": "next build",
"start": "next start"
},
......
After so many struggle, I found answer of my question I have to add following line in my package.json under scripts:
"scripts": {
......
"export": "npm run build && next export"
.....
},
Basically I my case, npm run build && next export was complete required command to build NextJS project. So after adding this into package.json you just need to run in terminal:
npm export
and it will generate complete build for nextjs project.
You have a package.json script called build that runs next build. The next build command by default builds the app for development, which doesn't create a production bundle.
In order to create the production bundle on your local machine, you need to specify that you're on production environment through NODE_ENV=production (this is done automatically in now and other deployment servers). Basically, your package.json would end up:
"scripts": {
"dev": "node server.js",
"build": "next build",
"start": "next start",
"prod:build": "NODE_ENV=production npm run build"
},
You can replace npm run build with next build directly if you prefer that.

Custom startup command for Node.js app on Azure with Babel 6

I'm having the same issue as this question which wasn't really resolved as the original questioner abandoned this route. I'm trying to run a node app on Azure using Babel6. The package.json file I'm using has the following in it:
"scripts": {
"start": "node ./node_modules/babel-cli/bin/babel-node.js server.js"
}
I've checked using the Azure console and the babel-cli node module is installed and the server.js file is in wwwroot. Despite this I get the following when I commit to Azure:
remote: Start script "./node_modules/babel-cli/bin/babel-node.js
server.js" from package.json is not found.
The npm version running on Azure is 3.10.3, node version is 6.6.0. Can anyone advise on how to get this up and running. Any help much appreciated!
It seems that to run node.js applications in ES2015 on Web Apps. We need to compile them to ES5 version, you can leverage Custom Deployment Script to achieve this.
Here is example repos on GitHub, which leverages gulp and custom deployment script to compile the node.js from ES6 to ES5 via the Azure deployment task.
Specially, this example defines several scripts in package.json for the deployment task calling:
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "gulp nodemon",
"build": "gulp build"
},
And manually call the npm scripts in deploy.cmd before KuduSync task move the deployment folder to production folder.
:Deployment
echo Handling node.js deployment.
:: 0. Select node version for build
call :SelectNodeVersion
:: 1. Install build dependencies
pushd "%DEPLOYMENT_SOURCE%"
call :ExecuteCmd !NPM_CMD! install --production
IF !ERRORLEVEL! NEQ 0 goto error
popd
:: 2. Run build command
pushd "%DEPLOYMENT_SOURCE%"
call :ExecuteCmd !NPM_CMD! run-script build
IF !ERRORLEVEL! NEQ 0 goto error
popd

Node.js with express - npm start do not work

I have problem trying to run node app in Windows 7.I have installed express generator globally and with it help created a test_app directory and installed the modules. In the package.json scripts look like this:
"scripts": {
"start": "node ./bin/www"
}
However, when i run npm start it just returns this in console:
test_app#0.0.1 start C:\work\test_app
node ./bin/www
If i try to run node app.js nothing happens. Before there wasn't problems with the express, until the moment when i had to install express-generate globally.

Categories