How can i include server/app.js in webpack-dev-server? - node.js

I'm building a client with react and am running npm start and in package.json my scripts look like this:
"scripts": {
"start": "webpack-dev-server"
}
But i've also code other server side code in server/app.js that I want to run and if I run node app.js then that runs but i'd like server/app.js to be included with webpack-dev-server so that when I run npm start server/app.js is also run.
Is that possible? I was reading up on the various options and at this stage after the simplest.
Thx.

Update your script as follows:
"scripts": {
"start": "webpack-dev-server && node app.js"
}

Related

nodejs run custom script before main script

I want to run ../my_dir/my_script.js each time when I run node index.js from actual app project folder.
In others words, I need my_script.js to be common in every app (A,B,C...N) and executing before index.js
Structure:
+my_dir
-my_script.js
+appA
+node_modules
-package.json
-index.js
+appB
+node_modules
-package.json
-index.js
my_script.js
console.log('my_script.js from parent directory STARTED');
package.json (similar for each appA, appB ... etc)
{
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
//I HAVE TRIED
"start": "node ../my_dir/my_script.js",
"postinstall": "npm run-script ../my_dir/my_script.js",
"preinstall": "npm run --prefix ../my_dir/my_script.js",
}
}
index.js (similar for each appA, appB ... etc);
console.log('index.js STARTED');
If I try inside appA node index.js I got index.js STARTED
If I try inside appA npm start I got my_script.js from parent directory STARTED
Expected: (running both):
my_script.js from parent directory STARTED
index.js STARTED
Any idea how to achieve that?
I don't think it is possible to automatically have a second script run when using the command line node command.
What you can do is either run both scripts manually
node ../my_dir/my_script.js && node index.js
or bundle them in your package.json
"start": "node ../my_dir/my_script.js && node index.js",
and execute with
npm start
Your postinstall and preinstall are not quite right, those get run when doing npm install (you can put the post and pre keywords before any command https://docs.npmjs.com/cli/v9/using-npm/scripts)
so you could also do
"start": "node index.js",
"prestart": "node ../my_dir/my_script.js"
and execute with
npm start
again

Connecting server in root package.json

I have a React app and here is my folder setup:
If I cd src and then run node server.js, I can see my server is running based off messages in added into console.log.
But when I'm in the root folder and I run npm start, the server.js is not connected. Here is my root package.json, what am I missing?
If you want to run react app and server simultaneously, you can use concurrently. Install it, and modify your scripts section so you will have separate entries for react-scripts start and node src/server.js. Then you can run them both with concurrently. It will look something like this:
{
"scripts": {
"start-react-app": "react-scripts start",
"start-server": "node src/server.js",
"start": "concurrently \"npm:start-react-app\" \"npm:start-server\""
}
}
This way, when you run npm start it will launch both scripts.

How to chain custom script in package.json to call prestart mongod?

Trying to streamline my package.json and local development with a custom script to run Nodemon. I'm currently building an app with a front and back end I need to call mongod before start and before my custom in two tabs however I'm running into an issue.
mongod will only run in the terminal if the terminal path is set to local from testing and I've read:
Correct way of starting mongodb and express?
npm starts to execute many prestart scripts
How to npm start at a different directory
How do I add a custom script to my package.json file that runs a javascript file?
I can use prestart as:
"scripts": {
"prestart": "cd && mongod",
"start": "node app",
"nodemon": "./node_modules/.bin/nodemon app"
}
but I'm not seeing how I should chain a prestart with a custom scripts. When I try to chain it with nodemon as:
"scripts": {
"prestart": "cd && mongod",
"start": "node app",
"nodemon": "cd && mongod && ./node_modules/.bin/nodemon app"
},
Nodemon is fired first than mongodb crashes in my package.json when I call Nodemon as:
npm run nodemon
How can I start mongod before starting nodemon in my development process through one command in the package.json?

nodemon to exec "prestart" script at package.json

I want to setup nodemon to run the "prestart" script when server restarts, the reason, I'm using webpack and I want to build bundle.js every time a file changes, I'm not using webpack-dev-server cause I don't know how to set it up with an existing node app running, I got my backend on node.js, help on that will be appreciated.
The way I've been working is: run npm start every time I make a change on the code, but that too much effort.
Here's the scripts object at package.json so that you have an idea of what's going on:
"scripts": {
"bundle": "webpack --config webpack.config.js",
"prestart": "npm run bundle",
"start": "node server.js"
}
The way I've accomplished to do that is by creating a nodemon.json file with the exec property:
{
// ... Other conf (see an example in https://github.com/remy/nodemon/blob/master/doc/sample-nodemon.md)
"watch": [
"app" // Set your directories/files to watch
],
"exec": "npm run prestart && node server/index.js"
}

Npm "scripts": "start" run express and open url

I have this start params in package.json
"scripts": {
"start": "node bin/www"
},
It is running my express app when I am typing npm start.
But I want browser opened http://localhost:8081 at the same time. How can I say to start to open my local url as well?
like: "start": "node bin/www, http://localhost:8081"
So when I am typing npm satrt it runs my express app and opens the url at the same time.
As far as I know it's like writing a bash command:
// Windows
"start":"start http://localhost:8081 & node bin/www"
// Mac
"start":"open http://localhost:8081 && node bin/www"
// Linux
"start":"xdg-open http://localhost:8081 && node bin/www"
For cross-platform support use open-cli.
Install it:
npm install --save-dev open-cli
Add it to your scripts:
"start": "open-cli http://localhost:8081 && node bin/www"
You just need to use start in the right order!
"start": "npm run dev & start http://localhost:8000",
Bad
"start": "start http://localhost:8000 & npm run dev",
Good

Resources