NPM command for run Node server after webpack finish building - node.js

I want to run a command like: npm run start:dev to make my node server runs but after webpack finished building bundles.
My current npm scripts are:
"scripts": {
"start": "node server/server.js",
"start:dev": "npm run build:prod & npm start",
"lint": "eslint *",
"build:dev": "webpack",
"build:prod": "webpack -p --env production",
"dev-server": "webpack-dev-server",
"test": "cross-env NODE_ENV=test jest --config=jest.config.json"
},
The current command will kick both operations together.

Typo: you don't actually want to use &, you want &&:
"start:dev": "npm run build:prod && npm start",
A single ampersand will background the run:build job and cause start to run concurrently.

Related

Custom script in NPM package.json is ignored, runs as standalone

in my package.json I have these scripts:
"scripts":
{
"lint": "./node_modules/.bin/eslint src",
"build": "npm run clean && npm run dev && npm run prod",
"tsc": "./node_modules/.bin/tsc",
"dev": "./node_modules/.bin/webpack --config webpack.dev.js",
"prod": "./node_modules/.bin/webpack --config webpack.prod.js",
"clean": "del /q inet-henge*.js* dist",
"watch": "./node_modules/.bin/webpack --config webpack.dev.js --watch",
"postinstall": "copy inet-henge.js \"..\\flask\\customer_topology\\static\""
}
But for some reason postinstall is completely ignored when i run npm run build. When I run it as standalone script, it runs, the file is copied to the new location. Path is correct. What could be the problem, I am on Windows
post<x> runs after <x> so postinstall runs after install and if you want a script to run after build it should be called postbuild See https://docs.npmjs.com/cli/v6/using-npm/scripts for more details.

How to add environment variable to server, run by cypress testing?

My package.json file is
"scripts": {
"start": "concurrently \"nodemon index.js\" \"PORT=3000 react-scripts start\"",
"build": "react-scripts build",
"server": "NODE_ENV=production nodemon index.js",
"dev": "NODE_ENV=development nodemon index.js",
"test": "react-scripts test",
"eslint": "eslint .",
"cypress:open": "cypress open",
"start:test": "NODE_ENV=test concurrently \"NODE_ENV=test nodemon index.js\" \"NODE_ENV=test PORT=3000 react-scripts start\""
},
my creation of router in node app.js is
if (process.env.NODE_ENV === 'test') {
const testingRouter = require('./controllers/testing')
app.use('/api/testing', testingRouter)
}
but when I run cypress test it complains that there is no such router. How can I make NODE_ENV=test while calling npm run cypress:open
command?
And how can I console.log(process.env.NODE_ENV) to see what if it was passed to cypres process?
solved
"start:test": "cross-env NODE_ENV==test concurrently \"cross-env NODE_ENV=test nodemon index.js\" \"cross-env NODE_ENV=test PORT=3000 react-scripts start\""
and
npm run start:test command in one command prompt, and run cypress in another

Start react-create-app and Electron.js with one NPM command

I have simple custom starter pack react-create-app and Electron.js.
I have added to package.json file:
"scripts": {
"electron": "electron .",
"start": "cross-env BROWSER=none react-scripts start",
....
and I can start Electron with npm run electron and React with - npm start.
What I want is to start React and Electron just with one command like: npm run both.
I have tried:
"both": "\"npm start\" \"npm run electron \"",
but I am getting an error in a log file:
Exit status 1 node_modules\npm\node_modules\npm-lifecycle\index.js:301:16)
- nothing specific
I have tried and:
"start": "npm run electron . && cross-env BROWSER=none react-scripts start",
, but this starts the Electron, when I close it, it start the React app.
Again error:
"electron": "electron .",
"start": "cross-env BROWSER=none react-scripts start",
"both": "\"npm run electron\" \"npm run start\"",
I don't know, how to start react-create-app and Electron with just one NPM command ?
Consider utilizing concurrently.
cd to your project directory and run the following command to install it:
npm i -D concurrently
Then redefine the both script in the scripts section of your package.json as follows:
"both": "concurrently \"npm start\" \"npm run electron\""
or the slightly shortened equivalent:
"both": "concurrently \"npm:start\" \"npm:electron\""

Node Application Heroku Application Error

I am using heroku/nodejs build pack.
this is how my scripts in package.json look like:
"scripts": {
"build": "webpack --config webpack.prod.js",
"server": "http-server public -p 3000 -a localhost -c 0",
"prod": "npm run build && npm run server",
"dev": "webpack-dev-server --config webpack.dev.js",
"lint": "eslint . --fix"
},
In procfile I have only one line of code:
web: npm run prod
Any ideas why am I seeing application error?
In regards to the error, I think that you should be using the environment variable $PORT i.e. http-server public -p $PORT -c 0 Make sure you have http-server in you deps and not your devDeps.
This might not be causing the error but you shouldn't do your build on run. Instead add the build step to a postinstall NPM script like below.
"scripts": {
"build": "webpack --config webpack.prod.js",
"postinstall": "npm run build",
"server": "http-server public -p $PORT -c 0",
"prod": "npm run server",
"dev": "webpack-dev-server --config webpack.dev.js",
"lint": "eslint . --fix"
},
This will ensure that your build gets run on deployment and not every time the node process starts.

Allocation failed - process out of memory

I am going to compile web-pack i have got issue in that i.e Allocation failed - process out of memory .I know i can increase the space size using this
--max_old_space_size=8192
i tried many ways to fix this but nothing is working.
here is my package.json
"scripts": {
"karma": "karma",
"test": "karma start",
"rimraf": "rimraf",
"tslint": "tslint",
"typedoc": "typedoc",
"webpack": "webpack --progress --profile --bail",
"webpack-dev-server": "webpack-dev-server",
"webdriver-manager": "webdriver-manager",
"clean": "npm cache clean && npm run rimraf -- node_modules doc coverage dist compiled dll",
"clean:dist": "npm run rimraf -- dist",
"clean:dll": "npm run rimraf -- dll",
"clean:electron": "npm run rimraf -- build",
"preclean:install": "npm run clean",
"clean:install": "npm set progress=false && npm install",
"preclean:start": "npm run clean",
"clean:start": "npm start",
"watch": "npm run watch:dev",
"watch:dev": "npm run build:dev -- --watch",
"watch:dev:hmr": "npm run watch:dev -- --hot",
"watch:prod": "npm run build:prod -- --watch",
"build": "npm run build:dev",
"prebuild:dev": "npm run clean:dist",
"build:dev": " npm run clean:dist && npm run webpack -- --config config/webpack.dev.js",
"prebuild:prod": "npm run clean:dist",
"build:prod": "npm run clean:dist && webpack --config config/webpack.prod.js --progress --profile",
"server": "npm run server:dev",
"server:dev": "webpack-dev-server --config config/webpack.dev.js --progress --profile --watch --content-base src/",
"server:dev:hmr": "npm run server:dev -- --inline --hot",
"server:prod": "http-server dist -c-1 --cors",
"server:test": "http-server dist-demo -c-1 --cors",
"server:prod:ci": "http-server dist -p 3000 -c-1 --cors",
"webdriver:update": "npm run webdriver-manager update",
"webdriver:start": "npm run webdriver-manager start",
"lint": "npm run tslint \"src/**/*.ts\" --force",
"ngc": "./node_modules/.bin/ngc-w -p tsconfig.webpack.json",
"pree2e": "npm run webdriver:update -- --standalone",
"pretest": "npm run lint",
"docs": "npm run typedoc -- --options typedoc.json --exclude '**/*.spec.ts' ./src/",
"gh-pages": "wintersmith build -C docs && gh-pages -d docs/build",
"start": "npm run server:dev",
"start:hmr": "npm run server:dev:hmr",
"version": "npm run build",
"postversion": "git push && git push --tags",
"build:electron": "npm run build:electron.full",
"prebuild:electron.full": "npm run clean:electron",
"build:electron.full": "npm run build:electron.renderer && npm run build:electron.main",
"postbuild:electron.full": "npm run electron:start",
"build:electron.renderer": "npm run webpack -- --config config/electron/webpack.renderer.prod.js",
"build:electron.main": "npm run webpack -- --config config/electron/webpack.electron.prod.js",
"electron:start": "electron build",
"build:aot:prod": "npm run clean:dist && npm run clean:aot && webpack --config config/webpack.prod.js --progress --profile --bail",
"build:aot": "npm run build:aot:prod",
"clean:aot": "npm run rimraf -- compiled",
"build:ci": "npm run build:prod && npm run build:aot"
},
and the command for running my package.json is
npm server
please help my out how i increase size.
I had similar issue. I was using node version 8.x. Downgrading it to 6.11.0 resolved my issue.

Resources