node packge.json scripts - add command to existing script - javascript

I'd like to add a command to the "start" script so when I do npm start, first thing that will run is npm install.
My package.json looks as follows:
.
.
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "DEBUG=my-app node src/index.js",
"dev": "nodemon src/index.js"
},
.
.
.
I thought about adding npm install inside the start script:
"start": "npm install DEBUG=my-app node src/index.js",
But this doesn't work so I'd like to get a suggestion, wether if it's even possible..

I think you only use the && conector. like:
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "npm install && DEBUG=my-app node src/index.js",
"dev": "nodemon src/index.js"
}

Andy yes, my app should be deployed once with a single command.
This is a quite a heavy/slow start, installing all modules before starting the app.
That means if I change the code somewhwere in my node server, stop the process and run it again, a full install is gonna happen. I realize that you have a dev script with nodemon, but still.
Another case: If your app crashes on the live server and you need to start it up again then a full install will happen. What happens if a module has gone up a patch or a minor version. That means you will start up a project with different dependencies.
If you're doing this in the ci/cd then a pipeline is usually split up:
Install - npm ci
Build/compile - for example if you have typescript (not in your case)
Run all tests
Remove the devDependencies with npm prune
Start up the process
What you would maybe do is have a script called "pipeline" or something, and then call that.
"pipeline": npm ci && npm run build && npm test && npm prune && npm start
This script would then be called in your pipeline code.

Related

How to chain multiple commands in npm package.json scripts

I am trying to create a script in my package.json file that will launch my nodemon app, then trigger a gulp sass watch
Currently, I can do this by running a npm launch which starts nodemon, then in a separate terminal window I can run gulp watch to trigger the sass watch from my gulp file.
I would like to create a single script command in package.json that will do both of these- is that possible?
package.json
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon app.js",
"launch": "nodemon app.js && gulp watch"
},
gulpfile.js
const { src, dest, watch } = require("gulp");
const sass = require('gulp-sass')(require('node-sass'));
function generateCSS(cb) {
src('./sass/**/*.scss')
.pipe(sass().on('error', sass.logError))
.pipe(dest('public/css'));
cb();
}
function watchFiles(cb) {
watch('sass/**/**.scss', generateCSS);
}
exports.css = generateCSS;
exports.watch = watchFiles;
edit: please see my answer below for what worked for me, inspired by the answer from #cmgchess
You should be able to use the npm-run-all package and its run-p command to execute multiple scripts side-by side:
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon app.js",
"watch": "gulp watch",
"launch": "run-p start watch"
},
Then you can just do:
npm launch
Just use the &&
package.json
"build": "npm run base && npm run utilities:global && npm run utilities:unstyled && npm run utilities:styled && npm run components && npm run merge:unstyled && npm run merge:styled && npm run rtl && npm run prejss && npm run themes && npm run full",
per #cmgchess answer in the comments, it turns out using a double ampersand runs the command sequentially. however, there is also the option to use a single ampersand, which will run them in parallel. In this case, this is what I needed:
"launch": "nodemon app.js & gulp watch"
edit: ok the first time I ran the launch it worked fine. however, subsequent launches crashed. I have it working again now over multiple launches, and what I had to do additionally is switch the order of the commands:
"launchreverse": "gulp watch & nodemon app.js"

Using nodemon and --inspect with serverless

I am writing a lambda service and hate having to stop and start the server after every change.
I do have this running together with chrome --inspect working correctly:
"start": "node --inspect-brk $(which serverless) invoke local -f getGoldenDeomondotcom"
I then used:
"start": "nodemon --exec \"node --inspect-brk $(which serverless) invoke local -f getGoldenDeomondotcom\""
to allow me to use nodemon with --inspect, however the messages don't pipe through to chrome.
Any ideas how I could use them together?
Make sure you use js or replace js in the code below with your language ext e.g py,ts
in your package.json
"scripts": {
"start:dev": "nodemon -e js --exec \"sls offline\""
},
From github ... (https://github.com/akupila)
nodemon --exec "serverless serve start"
I'm using this in package.json during dev:
"scripts": {
"start": "nodemon --exec \"serverless serve start\""
}
This allows me to just run npm start. Whenever any file in the project changes nodemon will restart serve.
I'm using serverless-offline and serverless-dynamodb-local, I just added
"start": "nodemon --exec \"sls offline start\"" to scripts

missing start script in NPM

First, sorry for my bad english.
I'm trying to deploy an app to Heroku using the command heroku local web in terminal, but I'm getting ERR! missing script: start, even though I have the script in my json file. And if I try to run npm start in terminal the same error appears. I searched for similar problems in Stack Overflow but I couldn't solve.
My .json is:
{
"name": "server",
"version": "1.0.0",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
.
.
My dependencies here
.
.
},
"scripts": {
"start": "node index.js",
"test": "echo \"Error: nko test specified\" && exit 1"
}
}
PS.: When I run npm install in terminal, the json file just erases the script and everything I wrote, for example I added "config" : { "port" : "3000" }, but after running npm install it just disappeared.
I think you confused yarn and npm
If you want to use yarn and if you package.json contain a key like start you can call yarn start
But, if you want to work with npm you need to use npm run start the run it's important because npm can't understand.

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.

Cannot find module '../lib/cli' error when running server

I am trying to run build a small express API. The problem is I am having problems trying to get the server up and running. When I type npm start I get the following error.
> nodemon ./index.js --exec babel-node -e js
module.js:557
throw err;
^
Error: Cannot find module '../lib/cli'
The script I have written in the in that package.json is the following.
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start":"nodemon ./index.js --exec babel-node -e js"
},
Any help would be appreciated.
I needed to reinstall the node_modules.
cd your_project_folder
rm -rf node_modules
npm install
There can be some issue with the node_modules. You must check your node_modules installation globally as well as locally inside project directory.

Categories