1

I cant get my head around how scripts are running within package.json & would appreciate some insight for us newbies.

Is it the case that they are bash scripts that are run by node having loaded the various dependencies?

If yes, then how does it process the javascript code?

Kayote
  • 14,579
  • 25
  • 85
  • 144
  • Hey mate this https://www.google.bg/webhp?hl=bg&sa=X&ved=0ahUKEwiT8L_t24LRAhWLVxoKHYH8Dc4QPAgD#hl=bg&q=how+does+package.json+work leads me to https://docs.npmjs.com/files/package.json which I see has a tons of info about `package.json` :) – codtex Dec 20 '16 at 14:11
  • 3
    Possible duplicate of [What is Node.js?](http://stackoverflow.com/questions/1884724/what-is-node-js) – Igor Dec 20 '16 at 14:12
  • `Node.js` runs JavaScript code from command line interface. `package.json` doesn't do anything, it just describes what should happen. `Node.js` parses `package.json` and reads it. It's capable of running JS, as I mentioned, which is where the magic comes from. – Mjh Dec 20 '16 at 14:14
  • The question is more about the flow that I am confused about. I am aware that `node` is `javascript`. More specifically, how is the script executed? Is it that the `bash` script is run by node? Does `node` understand `bash`? Or do they communicate with eachother? – Kayote Dec 20 '16 at 14:31
  • Bash script is ran by bash. Node invokes it. It's a simple system call, available to many languages (PHP uses `exec()` for such purpose), node is no exception. – Mjh Dec 20 '16 at 14:36
  • Have you read `man npm`? Which leads you to `npm help 5 package.json`? – slim Dec 20 '16 at 14:48

2 Answers2

3

Is it the case that they are bash scripts

yes

that are run by node

no, they are run by sh.

having loaded the various dependencies?

no, no js files are loaded, the only thing npm does for you is to prepare the environment. Among other things, it adds ./node_modules/.bin to PATH so you can invoke installed modules immediately.

When you run npm run-script whatever, this is what npm does:

  • reads the corresponding command line from package.json
  • prepares the environment
  • invokes sh (or comspec on win) and gives it the command and the env. No big magic here.
georg
  • 211,518
  • 52
  • 313
  • 390
  • Just for closure, sh is this -> http://superuser.com/questions/97614/what-exactly-is-the-sh-command – Kayote Dec 20 '16 at 15:04
0

This may not be 100% accurate so I implore other, more qualifies, experts to chime in.

NPM is a program, installed as part of the Node.JS environment. It's two main uses (as describe here) are for searching for node.js packages and installing node.js packages.

However, NPM is also capable of understanding "simple" (a relative term) scripts.

When you write a script in your package.json, and issue the NPM command, say "npm start", NPM will read and interpret the script. NPM then searches your node_modules structure for the accompanying binary and executes that binary with the necessary start parameters.

An example would be

"test": "mocha --reporter spec test"

when you issue "npm test", NPM will look for the mocha binary in your node_modules structure. NPM finds mocha initiates the call, passing the reporter command arg (--reporter spec) and the name of the file to be read and executed for the test.

Paul Stoner
  • 1,359
  • 21
  • 44