I'm using json-server as my local server for a nuxt project and i want to automatically launch the server and then run the project on another shell instance using "npm run dev"
in the scripts tag in package.json this is what i came up with :
"dev": "json-server --watch db.json --port 3004 & nuxt"
but this script only starts the server
try concurrently
npm install -g concurrently
"dev": "concurrently \"json-server --watch db.json --port 3004\" \"nuxt""
Related
I've recently tried to migrate our old site running on next 10 to the latest version 13. The site has a custom backend written with express. While running the project via concurrently I keep getting Module not found error as soon as I run the client on 3000. This is the scripts section("npm run dev" below is used to run the project):
"start_dev": "nodemon --inspect -w ./src/server -w ./src/server.js -w ./src/start.js ./src/start.js",
"build": "next build",
"client": "next dev",
"server": "npm run build && npm run start_dev",
"dev": "npx concurrently -k \"npm run server\" \"npm run client\""
Things seem to be working fine if I build the project first and then run client and server on separate terminals. But with this also, if I make a change in server that doesn't get picked up by client on the other terminal.
I'd highly appreciate if someone can give this a look as I'm stuck on the same issue for quite some time now. Please let me know if any further information is needed from my end here.
the problem is if the .next folder is generated by nodemon change, client side webpack hot reload cannot read .next folder
"scripts": {
"client": "next dev",
"build": "next build",
"server": "npm run build && npm run nodemon",
"start": "next start",
"lint": "next lint",
"nodemon": "nodemon -w ./src/server.js -w ./src/server ./src/start.js",
"dev": "concurrently -k \"npm run server\" \"npm run client\""
},
When you run npm run dev and refresh the page, this is how .next/pages folder. home index.js exists:
if I make a change in ./src/server.js after I see the home page on screen, .next folder will be recreated without index.js by nodemon change and if I refresh the page I will get error.
when you reload your page after change in server.js file you get error because index.js page does not exist. It does not exist because .next folder was not generated by webpack
When you first start the app, before refreshing the browser, if you make a change on server.js file, .next folder will be created and if you refresh the page you will get the same error. for some reason if .next folder generated by the nodemon change, client side cannot communicate by .next folder.
concurrently runs scripts in parallel, with npm run server you are calling next build and with npm run client you are calling next dev. So, you are calling next dev and next build scripts concurrently. Even though it says it is concurrently, I think it is not exactly concurrently. if you install npm i npm-run-all and add this script
"dev": "npm-run-all --parallel client server"
your app will never load, you will see flickering in a loop.
I have a package.json file at the root of my project folder which contains client and server folders.
in the root package.json I have the following scripts:
"scripts": {
"server": "npm run watch --prefix server",
"client": "npm start --prefix client",
"watch": "npm run server & npm run client"
}
but when i try to run npm run watch only the server script runs, what I don't understand is if I paste the contents of the watch script into the terminal it works just fine.
npm run <script> uses cmd.exe by default if you are on windows, and cmd does not support & to run commands in parallel, so it is better to use a package like npm-run-all.
I am working on angular 7 application. Below are the scripts in my package.json file to build the application
"scripts": {
"build:all": "npm run build:dev & npm run postbuild",
"build:dev": "ng build --extract-css --watch",
"postbuild": "node fileMover.js",
}
I want to run both build:dev and postbuild commands in sequence.
so first command builds the application by updating the dist folder with bundle files. To move the bundle files to separate folders, we have created a fileMover.js node script.
without --watch, build:all script works fine but due to this, we need to manually run the command everytime we make any changes & save through visual studio or other tool. With watch, it never runs the second "postbuild" command.
Is there any alternate to run both scripts (fist with watch) in sequence?
Thanks :)
You could change the & to && as a single & is for running in parallel and double is for one after-the-other, I believe.
scripts": {
"build:all": "npm run build:dev && npm run postbuild",
"build:dev": "ng build --extract-css --watch",
"postbuild": "node fileMover.js",
}
You could also use the 'pre' and 'post' affixes:
scripts": {
"build:dev": "ng build --extract-css --watch",
"build:all": "npm run build:dev",
"postbuild:all": "node fileMover.js",
}
I'm using typescript on my project and I can successfully watch + compile .ts files and output them to dist folder.
here is the scripts part of my package.json
"start": "npm run build && npm run watch",
"build": "npm run build-ts && npm run tslint",
"test": "cross-env NODE_ENV=test jest --watch",
"watch": "concurrently -k -p \"[{name}]\" -n \"Typescript,Node\" -c \"cyan.bold,green.bold\" \"npm run watch-ts\" \"npm run serve\"",
"serve": "nodemon dist/server.js",
"build-ts": "tsc",
"watch-ts": "tsc -w",
"tslint": "tslint -c tslint.json -p tsconfig.json"
The problem is I want to use js templating engine (nunjucks) and I need to watch the view files inside the views folder and move them to the dist folder.
Is there a way by just using npm scripts or nodejs?
Or do I need to use other tools like gulp or webpack?
I have the "same" request to for a CRUD graphql back-end server, but don't want to use gulp or webpack just to keep it simple.
I see that you use nodemon like me. Then, according the docs at https://github.com/remy/nodemon, it can be used it to monitor changes of any kind of file other than the default js. More over, nodemon can monitor the status of other transactional server other than node.
The first task is detecting the changes of wanted files: in my case I want copy the *.gql files in my src/schema folder to build/schema folder. For that, you can use the ext for the kind of files, and watch option for the source folder to explore.
The second one task is matter of copying the files. Naturally, you can use the copy command of your host OS. In my case I use the DOS xcopy command of the Windows shell (or cp in Unix like OS). nodemon has an "event-hook" with the event option, that can execute a command line when an event occurs. Just we need the restart event of the node server when the changes are detected for nodemon.
You can use the command line options, or a global config file, or in you local package.json project config file. I show up the last one using nodemonConfig section of package.json:
"nodemonConfig": {
"watch": [
"./src/schema",
"./build"
],
"ext": "js,gql",
"events": {
"restart": "xcopy .\\src\\schema\\*.gql .\\build\\schema /Y /O /R /F /I /V /E"
}
}
Ozkr's answer is great, I just want to add what worked for me, I had to change it a bit as nodemon was running into an infinite restart otherwise:
"nodemonConfig": {
"watch": [
"./views",
"./public"
],
"ext": "hjs,js",
"events": {
"restart": "cp -r views dist \n cp -r public dist"
}
}
copy-and-watch does just that:
I use this code to copy html files during development:
"copy_html": "yarn copy-and-watch src/mail_templates/* prod/mail_templates --watch --clean",
I'm trying to get the Hackernews 2.0 demo up and running on my Digital Ocean droplet, but I fail.
npm run start spins up the server on :8080.
npm run build builds for production.
The defined build tasks are defined here:
"scripts": {
"dev": "node server",
"start": "cross-env NODE_ENV=production node server",
"build": "npm run build:client && npm run build:server",
"build:client": "cross-env NODE_ENV=production webpack --config build/webpack.client.config.js --progress --hide-modules",
"build:server": "cross-env NODE_ENV=production webpack --config build/webpack.server.config.js --progress --hide-modules"
}
...and the entire repo is here.
But what should I execute to serve as a :80 website?
I asked on Gitter.im, on the Vue-channel, but with zero success.
Anyone?
You don't need to execute anything special. Simply change the definiton of listen port inside the application, on line 89 of server.js
const port = process.env.PORT || 80 // << changed here
or export env Var PORT prior to start. In linux, something like
export PORT=80 && npm start
EDIT:
or even create you own start script on package.json
"start80": "cross-env NODE_ENV=production PORT=80 node server",
(maybe- i dont' know exactly how cross-env works)