npm run does nothing - node.js

I've been working with Node.js/npm for a while, but I never used npm scripts. I was quite surprised to find that I can't get them to work at all on my Windows/Cygwin system. With a package.json like this ...
{
"name": "demo",
"scripts": {
"env": "env",
"hello": "echo Hello!",
"crap": "I am complete nonsense."
}
}
... all three npm run commands do nothing. npm run crap executes and returns immediately with an OK status (I tested with the -dd parameter); npm run doesntexist throws the expected error. Testing without Cygwin on the regular Windows shell made no difference.

I finally found out myself. There is an npm setting with which you can stop all npm scripts from running. For some reason, my userconfig file ~/.npmrc contained the setting ignore-scripts = true. If you run into this problem, check npm config list.

Related

ERR Missing script critical

I am trying to generate critical css path using node module by addy osmania (https://github.com/addyosmani/critical)
i have below code in package json
"scripts": {
"criticalcss": "node criticalcss.mjs",
}
when i do npm run critical i get bellow error...
npm ERR! Missing script: "critical"
What am i doing wrong? My node installation is in c:user/admin and my production files are in xampp htdocs
i used node script.mjs and it worked
Your script is named criticalcss and you are running the command npm run critical.
Try
npm run criticalcss

How to print out multi-line message from npm script

Is it possible to print out a multi-line message from an npm script?
My team has an alternative script that needs to run in place of npm publish.
So instead of running npm publish from the terminal, we should be running npm run publish:lib. But, some team members forget about the alternative script and end up just running npm publish.
So what I've done so far is added this to my package.json:
"scripts": {
"postpublish": "node -e \"console.log('Did you mean npm run publish:lib?')\"",
}
That way when they do run npm publish, they'll be reminded about the alternative script. However, I'm not satisfied with it because it's not very noticible, and also the words console.log get's printed out, which is kind of ugly.
My goal is to have something printed out that looks like this:
The solution I ended up using was the following:
package.json
"scripts": {
"postpublish": "node publishWarning.js"
}
publishWarning.js
console.log('***************************************')
console.log('***************************************')
console.log('** Did you mean npm run publish:lib? **')
console.log('***************************************')
console.log('***************************************')

What is the difference between npm start and http-server?

I am a beginner in web development. These two confuse me. If they both open up a page in localhost then why do I need to install http-server instead of just using npm start?
npm start runs whatever command is specified in the "start" script in your package.json. From the npm docs:
This runs an arbitrary command specified in the package's "start" property of its "scripts" object. If no "start" property is specified on the "scripts" object, it will run node server.js.
https://docs.npmjs.com/cli/v6/commands/npm-start
So if your package.json contains the following:
{
"scripts": {
"start": "echo Hello"
}
}
Then running npm start will print "Hello". The npm start script is not an executable itself; it just runs whatever is specified in your package.json.
http-server on the other hand is a specific executable that starts an HTTP server. It may refer to the http-server npm package, or a different script with that name available in your command line interface.
npm start is a convention often used by other tools, e.g. testing or continuous integration, to "start up" your app regardless of what technology it is using. A common set up would be to specify the specific startup script in your "start" script:
{
"scripts": {
"start": "http-server"
}
}
While that makes npm start and http-server do the same thing in your project directory, other tools will rely on npm start since otherwise they wouldn't know that you wanted to use http-server as your startup script.
http-server is an HTTP server written in JavaScript for Node.js.
npm start runs the start script specified in package.json. It might run a web server (which might be written using http-server) and open a browser to it. It might do something else. It's entirely configurable.

Confusion with npm scripts

I am trying to figure out how the https://github.com/fastify/fastify-example-twitter/blob/master/package.json works.
Specifically, it has a script entry as: "start": "fastify index.js".
However, there is no requirement to install fastify globally. Nonetheless, npm start is working fine. It does start fastify, while doing it from the shell results in: -bash: fastify: command not found
What is happening when npm start is invoked? Why I cannot run this from the command line, while npm runs this script command just fine.
If your dependency appears in an npm script command, the executable is added to your path.
From the npm scripts documentation:
If you depend on modules that define executable scripts, like test
suites, then those executables will be added to the PATH for executing
the scripts. So, if your package.json has this:
{
"name" : "foo" ,
"dependencies" : {
"bar" : "0.1.x"
} ,
"scripts": {
"start" : "bar ./test"
}
}
then you could run npm start to execute the bar script, which is
exported into the node_modules/.bin directory on npm install.
fastify is listed as a dependency, and as such can be ran as an npm script. The same goes for mocha, standard, and snazzy. None of them need to be globally installed, but are ran via their npm scripts.

Running npm scripts conditionally

I have 2 main build configurations - dev and prod.
I push updates to a heroku server that run npm install --production to install my app.
In the package.json I have the following segment:
"scripts": {
"postinstall": "make install"
}
that runs a make file that is responsible for uglifying the code and some other minor things.
However, I don't need to run this makefile in development mode. Is there any way to conditionally run scripts with npm?..
Thanks!
You can have something like this defined in your package.json (I'm sure theres a better shorthand for the if statement.)
"scripts": {
"postinstall":"if test \"$NODE_ENV\" = \"production\" ; then make install ; fi "
}
Then when you execute npm with production flag like you stated you already do
npm install --production
it will execute your make install because it will set $NODE_ENV = production
When I need to conditionally execute some task(s), I pass environment variables to the script/program and which takes care of that logic. I execute my scripts like this
NODE_ENV=dev npm run build
and in package.json, you would start a script/program
"scripts": {
"build":"node runner.js"
}
which would check the value of the environment variable to determine what to do. In runner.js I do something like the following
if (process.env.NODE_ENV){
switch(process.env.NODE_ENV){
....
}
}
For conditional npm scripts, you can use cross-platform basic logical operators to create if-like statements ( || and && ).
I've run into needing to run scripts to only generate helpers once on any machine. You can use inline javascript to do this by using process.exit() codes.
"scripts": {
"build":"(node -e \"if (! require('fs').existsSync('./bin/helpers')){process.exit(1)} \" || npm run setup-helpers) && npm run final-build-step"
}
So for testing envs you might do:
"scripts": {
"build":"node -e \"if (process.env.NODE_ENV === 'production'){process.exit(1)} \" || make install"
}
Can't you add another section in your .json under devDependencies? Then if you do npm install it'd install the packages specified under devDependincies and npm install --production would install the regular dependcies.
I would encourage you to take a different tack on uglifying your code. Take a look at connect-browserify or the even more powerful asset-rack.
These can automatically uglify your code on launch of the Express server, rather than on install. And you can configure them to do different things in development and production.

Resources