What is the difference between npm prestart and npm poststart? - npm

I am familiar with npm start, but I don't know what does the prestart and poststart do exactly.
Which script is runned before start (i.e.: pre or post). I wanna know to proper flow of it...?
After doing some research I found prestart is called first than start is called.

When we set up our scripts properly then simply running the command npm start will be able to handle all of our needs in the proper order.
It's has huge useful for many situations where we need something to happen immediately before or immediately after a primary action.( npm start) Here You have reference.
Example
{
"scripts": {
"prestart": "node first.js",
"start": "node index.js",
"poststart": "node last.js"
}
}
prestart starts first before npm start and poststart is fired last.

Related

trouble with npm preinstall script

I'm trying to make the jump to pnpm from npm. I found a helpful hint to keep from running "npm install" after I make the change as described here: https://pnpm.js.org/en/only-allow-pnpm
Unfortunately my preinstall lifecycle override doesn't get executed. Seems to simple enough but npm install still works when I run something like "npm install #types/jest"
package.json:
{
"name": "react-sandbox",
"version": "0.1.0",
"private": true,
"scripts": {
"preinstall": "npx only-allow pnpm"
}
}
npm version 6.14.2.
Any ideas?
Unfortunately, the preinstall script is executed only during argumentless installation. So when you run npm add #types/jest, that script will not be executed, thus npm won't be prevented from running.
But it will fail when running npm install.
As of now, there is no other way to prevent npm from execution.

How to do npm scripts work vs direct commands?

I keep seeing this behavior, so I want to understand it. I install gulp and the gulp-cli (not globally). Typing the gulp command gives me -bash: gulp: command not found
I take the gulp command and drop it into an npm script and boom, it works. Can someone explain what's going on here?
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"gulp": "gulp", // This works!!
"gulp-build": "gulp build" // As does this!!
},
Ahh, that makes sense. I was looking for the relative path to gulp in the project couldn't find the right path. Indeed ./node_modules/.bin/gulp does work. And thank you RobC for quoting that telling line in the docs. Totally makes sense now!

How to conditionally run npm script

I am looking for a cross-platform way of conditionally running a step in my build script.
I have a build step that is expensive, however checking to see if it is necessary is relatively quick. I have created a script that returns a non-zero error code if it is not necessary to do the build step. How can I write my npm script to only run the build if the check passes but provide errors if the build step fails?
Example package.json:
"scripts": {
"schema:build": "npm run schema:rebuild-check && npm run schema:force-build",
"schema:rebuild-check": "node tools/schema is-rebuild-necessary",
"schema:force-build": "npm run schema:validate && npm run schema:generate-index && npm run schema:bundle"
}
The problem with the above is that if the rebuild is not necessary my entire build fails. I can't just swallow the exit code with something like exitzero because I want to know if any of the commands fail in the schema:force-build script.
The problem arises from two things:
1) If an npm script exits with a non-zero code npm will always complain
2) Need to provide an "else" that returns non zero using the || operator
Working solution:
"schema:build": "node tools/schema is-rebuild-necessary && npm run schema:force-build || echo Skipping schema build",
"schema:force-build": "npm run schema:validate && npm run schema:generate-index && npm run schema:bundle"

What does npm run bundle do?

I'm trying to understand and follows https://github.com/DMPRoadmap/roadmap/wiki/Installation
But I don't understand something they use.
What does these do?
1) npm run bundle
I know it equals to npm run-script bundle as according to npm doc about run script but I don't really understand where the bundle come from; in other words, I don't understand what npm doc about run script mean by
an arbitrary command from a package's script object
2) npm run bundle -- -p
Since I don't know where the bundle come from, I don't know how to work out the meaning of -- -p option. I want to find its documentation and see the details.
I'm not sure if npm doc about bundle is related, but it seems to be replaced by install as documented in npm doc about install.
And why is this option got so many - characters (3 in this case) before p? I normally see 2 - for long option name and 1 - for abbreviated option name
Any time you see npm run [x] anywhere it means that it's executing a command located in the scripts section of the package.json file. Therefore npm run bundle runs the bundle command located here: https://github.com/DMPRoadmap/roadmap/blob/master/lib/assets/package.json#L8 which in this case looks like all it's doing is running webpack
"scripts": {
"test": "./node_modules/.bin/karma start",
"bundle": "./node_modules/.bin/webpack",
"lint": "./node_modules/.bin/eslint --ext .js --cache ./javascripts/ || true"
}

IntelliJ stuck after running npm scripts

I have created a Scala, Play project. I am trying to add Angular2 in it. I added two npm commands through edit configuration. They are suppose to install the required packages and use webpack to bundle final JS. I notice that nothing happens after 2nd script is executed (I do not know if that script is hung or there is some other issue (see pic). It seems that the 2nd npm script is stuck because on stopping the run command, I see exit code 1 - Process finished with exit code 1
Is there a way to find out if Intelli build/run process is still running?
The issue was with the 2nd script (npm start). I had to remove --profile --watch flag from the webpack command. This works - "scripts": {
"start": "webpack --config webpack.config.dev.js --progress"
}