Can I assume `node` is always in the $PATH setup by NPM? - node.js

So I have have been using NPM as a simple build tool for a few years where I can assume that every CLI tool installed through package.json is available in the PATH, as NPM adds the ./node_modules/.bin path to it.
But as I was about to write a small Node script to do some house cleanup chores I got a bit wary if NPM actually has the node executable in the PATH it gives to users. This might seems stupid, as all the scripts in ./node_modules/.bin rely on node being there for the wrapper scripts on Unix to be able to find it, but I thought there might be some magic going on in Windows. Perhaps they use some other magic of their own. Or something. Does not matter, really, I just want to be able to assert:
Is the node executable always on the PATH given to npm scripts by NPM? On all platforms? And is it always the same Node as NPM runs on?
This might seem too basic to almost be asked, but better safe than sorry.

Note: not a definitive answer, but too lengthy for a comment
If you look at the documentation on default values that are set up for scripts:
npm will default some script values based on package contents.
"start": "node server.js":
If there is a server.js file in the root of your package, then npm
will default the start command to node server.js.
Although this isn't a definitive answer to your question, npm uses node as a default in case there's no npm start defined. This is a clear indicator npm relies on node to be present under all circumstances.

On Windows, you can safely make the assumption that npm requires node to be in the PATH, or in the same folder as npm. This is from npm.cmd (the script that is called when you type npm):
SET "NODE_EXE=%~dp0\node.exe"
IF NOT EXIST "%NODE_EXE%" (
SET "NODE_EXE=node"
)

Related

What happens under the hood when you call `npm run` to run a NPM script?

I would like to know more about how NPM scripts work.
For example:
package.json
"scripts": {
"build": "set NODE_ENV=production&& webpack --config webpack.config.js",
}
When I execute npm run build:
What happens? I know a Node process will be initiated somewhere and webpack binary file will be called, right? But what are the steps before that? Does that depend whether I'm on Windows, Linux, PowerShell or Git bash? How does that process relate to the OS and the CMD/CLI that is being used?
The npm source code helpfully has the whole run-script functionality separated into its own module and repository so you can review the source code if the documentation does not answer your questions.
Speaking of your questions:
I know a Node process will be initiated somewhere and webpack binary file will be called, right?
The webpack executable will be run. And since webpack is a Node.js script, it will be run with node.
If, however, your "build" value consisted of shell/CLI commands, those commands would be run. Node.js is not necessarily invoked.
But what are the steps before that?
The "steps before that" include certain lifecycle scripts that might also be defined. In particular, if there is a "prebuild" script, it will run before the "build" script.
Does that depend whether I'm on Windows, Linux, PowerShell or Git bash? How does that process relate to the OS and the CMD/CLI that is being used?
npm (and node) make efforts to make Windows and Linux experiences comparable. Differences occur for sure, but without more details, I'm not sure I'd want to speculate about what specifics might be of interest to you beyond that.
Powershell and GitBash: Again, npm will make efforts to smooth out differences, but I'm sure they come up. One thing to be aware of is that your PATH (and other environment variables) might be set differently and that may affect behavior. (It may especially impact which version of node gets executed, if you have more than one version installed.)
I am not a Windows expert, but I have seen a lot of npm scripts that assume a UNIX-like environment. So, if given the choice and all else being equal (which it never is), a bash-like environment is probably going to be a bit smoother.
The answer I was looking for was something like this:
When you call npm run, npm will initiate a shell to run those instructions.
And which shell npm will use is defined in your npm settings.
You can see your npm config by running:
>>> npm config ls
In the example above, npm will run the script on git-bash.
You can change the npm shell by calling:
npm config set script-shell "C:\\Program Files\\git\\bin\\bash.exe"

I need to add dependency in my package.json and load module , after taking user input while running npm install

I need to add a dependency in my package.json and load module, by taking user input in command prompt while running npm install.
Is it possible to do so.
As you haven't provided much information about what exactly you try to accomplish, it's difficult to advise you or give a concrete example. Anyhow, in package.json, you can override the default behavior of npm scripts with the scripts field:
"scripts": {
"install": "./scripts/install.sh",
}
As someone else has noted before, you cannot call npm install in your custom script, though, as this would lead to infinite recursion. Thus, rather provide a preinstall script, which npm runs before install.
For use cases other than adding dependencies based on user input, I recommend to check how to set the environment variables and configurations, which the user then could override with npm config set project-name:config-name config-value.

How does NPM start an Angular and Typescript application?

Would it be possible to get an explanation of how npm start works?
I have an Angular 2 application written in Typescript. I enter npm start in the console and this both compiles and launches the application using Node's built in web server.
However what is the starting point for this? Which files does the npm start command read in order to compile and start the application? Also why do I use npm start and not e.g. node start? I understood that NPM was just the package manager for node itself.
What I understand so far:
I have a tsconfig.js file which tells the TypeScript compiler what to do.
I have a packages.json file which tells node which packages to download.
Where do the following files fit into this:
main.ts
app/app.module.ts - which I understand is the starting point for my application.
How do all of these components fit together to form the application?
npm start is just an alias for npm run start, which runs whatever command is in scripts.start in your package.json.
It neither knows nor cares about TypeScript, or Angular, or anything like that - it just executes whatever script it's given on the command line.

Specify the path to node when running executable npm moduels

I'm fairly new to node and npm so imagine this might be the way I'm trying to use it. My understanding so far and questions are as follows.
When installing modules with npm if they have executables they are created in node_moadules/.bin
For example npm install istanbul creates
node_moadules/.bin/istanbul.cmd with the following contents
#IF EXIST "%~dp0\node.exe" (
"%~dp0\node.exe" "%~dp0\..\istanbul\lib\cli.js" %*
) ELSE (
#SETLOCAL
#SET PATHEXT=%PATHEXT:;.JS;=;%
node "%~dp0\..\istanbul\lib\cli.js" %*
)
This seems to be roughly similar for other npm modules installed.
1) When would there be a node.exe in the bin directory (%~dp0 is the path of the batch file - so node_moadules/.bin)? When using npm istanbul it will use the above script but I don't understand when it would use the IF block, it seems it would always go into the ELSE which uses node on the path to run it. This will work as intended when I have node installed and on my path. However this would seem to be a problem if you don't have it on your path or need to use a specific version in a project (different from the version on your path).
2) I can fix this by manually editing the above script to point to the specific node version but don't want to have to do this for all module executables installed with npm. Is there a way to configure this in npm so it uses a specific path to node when it generates these executable files?
A bit more background
We're using the gradle-node-plugin in our build so we don't have to manage node and npm on our build machines, this does it for us. However this means we don't have node on the path.
The plugin allows me to install npm modules but there is no clear way to then run the executables, using the version of node unpacked by the plugin, without manually editing the generated executable scripts as in 2), or instead of calling npm istanbul calling something like this:
<path_to>\node <path_to>\node_modules\istanbul\lib\cli.js
This also doesn't seem right as I'm hardcoding to the cli.js executable path within the module which could presumably change in future versions of that module.
If anyone could explain this, or tell me where the specific part of the docs that do are that would be helpful. I've tried to search but probably not using the right terms.

What does npm mean for socket.io/installing stuff?

I'm a real noob at this. I've just began scratching the surface on node.js/socket.io/html5 and stuff. I finally figured out how to use my command prompt (using windows) to launch a "hello world" application with the command "node example.js." But what does npm mean? When I'm looking at socket.io it says to install, npm install socket.io Does that mean I need to extract all the files into my nodejs folder?
I'm confused.
npm is a generally awesome program for managing packages and dependencies (especially while you have a network connection). It does lots of fancy things and is most commonly used with node.js projects. That said, it's an unfortunately common misconception that the acronym stands for "node package manager".
In reality, npm doesn't actually stand for anything as it's not an acronym. With taglines like "no problem, meatbag" the npm organization playfully resists the trend of acronymization. It should always be referenced in lowercase to avoid confusion with the National Association of Pastoral Musicians.
npm is a command line interface program to manage node.js libraries (it stands for node package manager - at least it did initially, they since turned this into a bit of a running gag - thanks to #spex in the comments for that link). Check out the docs, it is awesome and amazing. As you mentioned, just type npm install in a command prompt, and voilĂ , you have the library in your local node modules.
npm is stands for Non-Parametric Mapping utility written by Chris Rorden...
But we can keep it as Node Package Manager and it is very helpful for Module Loader which uses CommmonJS module pattern, and I am not sure will Node will support the ES2015 Module Loading Syntax (Built in module Loading Syntax)?
for those who are wondering the same thing, you just type npm install socket.io in the command prompt where nodejs file is.
C:/Program Files/nodejs
That should be what your command prompt should say and then just type that in.

Resources