Using the Web3.js Library
The whole process can be done, from start to finish, within a few minutes.
Setting Up The Project
Create a directory, or a folder for your project, if you don't have one already.
We are assuming here that you want to connect to Solana from within an npm project.
Enter the project's directory or folder and run the command npm init.
Now save the library you'll need to access the Solana network: solana/web3js, using the command npm install.
npm install --save @solana/web3.js
Now create a script inside your project folder. It can have any name.
Inside the script, require the library, and then print the output of the variable.
const solanaWeb3 = require('@solana/web3.js');
Connecting to the Solana Network
You can run your script with the command node scriptname, replacing scriptname with the name of your script.
My script was named sol.js, so I ran it with this command, from outside my project directory:
Solana Network Output
You should now connect to the Solana network and see its output.
While the page of output will extend past a single screen, a few sample lines are shown below.
Account: [class Account],
Authorized: [class Authorized],
You have now established a connection to the Solana network.
For more information, see the Solana web3.js library's documentation:
Feel free to adapt and reuse this code in your own projects.