Node JS
Contents of this page:
- Sample code
- Useful tools
- Single-threaded non-blocking
- REPL
- Misc
- Global scope
- Package management - npm vs yarn
- Updating npm dependencies
- Upgrading node - Troubleshooting
- Node Js and WSL
- Debugging node in Visual Studio
- Node, proxies and IP addresses
Sample code
- CBF PRIVATE (Accessible to Clare only):
- [Directory of all sample CBF Node code](https://github.com/claresudbery/cbf-sample-solutions/blob/main/README.md#node]
Useful tools
Single-threaded non-blocking
- Node can also have separate worker threads so is not always single-threaded, even though it’s often described that way.
- The reason a single thread can be non-blocking is that it is v good at using the same thread to switch between different jobs. And single thread is more performant than having a separate thread for each user.
- More here
- … and here
REPL
- Node has its own
repl
. Just typenode
on the command line and hit Enter.
Misc
- For more information on syntax and available functions view the resource list:
Global scope
- Node.js is different from browser JavaScript when it comes to global scope.
- In web browsers the global scope is the window object, which is at the top level.
- In a browser if you define a variable using the
var
,let
orconst
keywords this will declare it as a global object as everything runs from the window. - Node is different. The top-level scope is not the global scope.
- Declaring a variable inside Node.js will not add it to the global scope. It will be local to that module.
- To add something to the global scope you need to export it, or add it to the
global
object.
Package management - npm vs yarn
Yarn
andnpm
are both package managers -npm
was specifically created for node.Yarn
was created by Facebook engineers, and is described as simply “javascript package management” but I think maybe it was also created specifically for node?create-react-app
will create a different project structure depending on whether you haveyarn
installed or not (more info here).- If you have
yarn
installed, you don’t have to useyarn
. You can still install and run projects that usenpm
rather thanyarn
. - If a project is set up to use
yarn
, you won’t be able to use it withnpm
. You’ll have to installyarn
(but that’s quick and easy, so shouldn’t be a problem).
Migrating from npm to yarn
- If you want to move a project from
npm
toyarn
, do the following:- Install
yarn
:npm install -g yarn
- Delete the
node_modules
folder - Delete
package-lock.json
(npm
’s lock file) - Run
yarn
on the command line in the root folder of the project- This is the equivalent of running
npm install
- It will create a new
node_modules
folder, which will have the same structure on all machines (not necessarily so withnpm
) - It will also create a new
yarn.lock
file - Edit your
readme.md
to explain that new users need to runyarn
now instead ofnpm install
- This is the equivalent of running
- More info here
- Install
Updating npm dependencies
Good explanation here.
Upgrading node - Troubleshooting
Windows
- I used the Windows installer, but couldn’t run node commands on the command line even after restart.
- It turned out only my system path had been updated and not my user path.
- Solution:
- Use Windows button to search for “environment variables” and select “Edit the system environment variables”
- Click Environment variables
- Find the path variable under user variables. Select it and click Edit…
- If you can’t see
C:\Program Files\nodejs
orC:\Program Files (x86)\nodejs
listed, click New and add it (check your file explorer to see which version is correct).
Mac
- If you try to upgrade node, you might find yourself in a situation where despite the upgrade apparently being successful, you are still seeing the wrong version when you run
node -v
ornode --version
on the command line. This happened to me, and it turned out to be all aboutnvm
. Here are my notes:- Node upgrade details here
- I first used the MacOS Installer, but it didn’t work:
- It said it had installed, but when I followed it up with
node --version
in Terminal, it was still saying I was on13.10.1
. - This remained true after restarting iTerm and then restarting my macbook
- It said it had installed, but when I followed it up with
- So then I tried installing via
n
(as recommended on this page)- …using these commands:
npm cache clean -f
npm install -g n
sudo n stable
- …but I still got
13.10.1
when I rannode -v
. But then I noticed the feedback from the previoussudo n stable
command:installed : v14.17.1 to /usr/local/bin/node
active : v13.10.1 at /Users/[username]/.nvm/versions/node/v13.10.1/bin/node
- It turns out I had
nvm
installed on my system (node version manager), which had the active version set to13.10.1
. The solution was to usenvm
to change versions, like this:nvm install 14.17.1
- I first used the MacOS Installer, but it didn’t work:
- Node upgrade details here
Node Js and WSL
- You’ll need WSL 2, which you’re better off running in a VM as long as it’s still part of the Insider programme.
- Then follow the instructions here.
Debugging node in Visual Studio
attaching directly via VS Code terminal
node --inspect program.js
where program.js
is your entry point - in one project I worked for it was server.js
(didn’t actually need the .js
- could just put server
).
- Use Cmd + Shift + P to bring up the Visual studio code command palette and type in “toggle auto attach”. If you’re already debugging, you can turn auto attach on and off via the orange status bar at the bottom.
More here
Attach to node process
The Attach to Node Process action opens a Quick Pick menu that lists all potential processes that are available to the Node.js debugger. Access it with Cmd + Shift + P to bring up the Visual studio code command palette and type in “Attach to Node Process”.
Via npm
If you already have an npm command setup - eg in this example a server
command in the scripts
section of package.json, then the config below would go in launch.json (more here).
Whenever you add a new config to launch.json, you get a new item in the dropdown top left next to the green Play / Debug button.
,
{
"name": "Launch via npm",
"type": "node",
"request": "launch",
"cwd": "${workspaceFolder}",
"runtimeExecutable": "npm",
"runtimeArgs": ["run-script", "server"],
"port": 9229
}
Node, proxies and IP addresses
-
When you add an entry to the hosts file, you change the TCP request to convert a name into an ip address (eg this in your hosts file: “127.0.0.1 acme” … means that “acme” is converted to the IP address)
-
But the thing you type into the browser is the thing that ends up in the host header on the http request (regardless of what’s in the hosts file)
-
Therefore you want to type
acme
, NOT127.0.0.1
-
This is because there is a node proxy running because we ran
make start-proxy
which usesindex.js
to analyse the http host header (see the makefile to see where thestart-proxy
command is configured).start-proxy
listens to your browser, checks the url, and sends it to the correct proxy (healthystockport or stockportgov) based on the url.
-
Index.js example here (accessible to Clare only), in the
proxy
sub-folder of the web-app project folder.-
If you look at Index.js (in the proxy folder in webapp code), it says listen on port 5555 and then sets up the correct business Id based on that.
-
It then routes requests to port 5000.
-
-
Makefile example here (accessible to Clare only), in the web-app project folder.
-
It then extracts the business Id (Eg “acme”) from the host header
-
Be aware that the node proxy will not come into the equation unless you have added your url AND PORT NUMBER into the proxy exceptions in your browser settings
Troubleshooting proxy settings
If you get a “connection refused” error when trying to visit your app in the browser:
-
Change your proxy settings in Firefox:
-
Go to Settings Options Advanced Network - Click the Settings button in the “Connection” section
- Make sure “Manual proxy configuration” is selected
- Set the http proxy and port. To find the values you need, do this:
-
- Search for Network Proxy Settings in Windows
-
- Under Manual Proxy Setup, you will see an IP address and port number – these are the ones you want
- Check the checkbox “Use this proxy server for all protocols”
- Add whatever is needed to your list of exceptions (The “No Proxy for” box)
-
-
Change your NO_PROXY environment variable (Windows System Environment variables) - It should contain the same list as in your “No Proxy for” setting (see above)
- Check your hosts file