Learning how to use Node.js with sublime text IDE version 3 enables you to enjoy the efficiency of the IDE. Apart from developing applications quickly, Sublime Text lets you run over 20+ programming languages on it.
First, you will learn how (Ubuntu) Linux installs packages. Next, you will install Node.js from the source. Then, install Sublime Text 3 and run Node.js on the IDE. What is more?
Find out below.
Getting started - Using Node.js with sublime text IDE
This section teaches how packages get installed on (Ubuntu) Linux to ease debugging Node.js or Sublime Text download-related issues.
Linux distributes installation files as packages. A package manager collects the package containing the target program and its dependencies (installed separately).
The most user-friendly package manager for Debian-based distributions like Ubuntu is the apt (Advanced Package Tool). Its alternatives are apt-get and dpkg.
apt traces the packages in the distribution's list of repositories. Besides, you can gather the packages from their respective repositories and add them to the package manager's list of repositories stored in /etc/apt/sources.list
or /etc/apt/sources.list.d
files.
Apart from apt, the application itself could have a package manager. For instance, Node.js has npm
(Node package manager) for installing and uninstalling packages. Also, you can use nvm (Node Version Manager) to manage various Node.js versions on your system.
Here is how to use the package managers.
Step-1: Install Node.js
Does Node.js exist?
We can check the binaries.
which node
which npm
or Node.js and npm versions.
node -v
npm -v
Output
If none of the above methods locates Node.js or npm, you have not installed Node.js on your machine.
user@hostname:~$ which node
user@hostname:~$ which npm
user@hostname:~$ node -v
Command 'node' not found, but can be installed with:
sudo apt install nodejs
user@hostname:~$ npm -v
Command 'npm' not found, but can be installed with:
sudo apt install npm
Let's install Node.js right away.
Install Node.js from the source
Here, we will install Node.js from the source because it allows us to choose a Node.js version to install.
Update and upgrade the system in readiness for a new installation.
sudo apt update
sudo apt upgrade
Download Node.js v18 and above from the sources.
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
The system adds the sources to the repositories.
# Adding the NodeSource signing key to your keyring...
+ curl -s https://deb.nodesource.com/gpgkey/nodesource.gpg.key | gpg --dearmor | tee /usr/share/keyrings/nodesource.gpg >/dev/null
gpg: WARNING: unsafe ownership on homedir '/home/user/.gnupg'
## Creating apt sources list file for the NodeSource Node.js 18.x repo...
+ echo 'deb [signed-by=/usr/share/keyrings/nodesource.gpg] https://deb.nodesource.com/node_18.x jammy main' > /etc/apt/sources.list.d/nodesource.list
+ echo 'deb-src [signed-by=/usr/share/keyrings/nodesource.gpg] https://deb.nodesource.com/node_18.x jammy main' >> /etc/apt/sources.list.d/nodesource.list
Then, it updates the repositories before telling us how to install Node.js on the system.
## Run `sudo apt-get install -y nodejs` to install Node.js 18.x and npm
## You may also need development tools to build native addons:
sudo apt-get install gcc g++ make
## To install the Yarn package manager, run:
curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | gpg --dearmor | sudo tee /usr/share/keyrings/yarnkey.gpg >/dev/null
echo "deb [signed-by=/usr/share/keyrings/yarnkey.gpg] https://dl.yarnpkg.com/debian stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt-get update && sudo apt-get install yarn
Let's install Node.js using the specified command.
sudo apt-get install -y nodejs
If your system lacks the curl tool, install it with this command.
sudo apt install curl
Verify the installation by checking node and npm versions.
node -v
npm -v
Now that we have installed Node.js, we can install Sublime Text before learning how to use Node.js with sublime text IDE.
Step-2: Install Sublime Text IDE
sudo apt update
sudo apt upgrade
Import the GPG keys for Sublime Text 3.
curl -fsSL https://download.sublimetext.com/sublimehq-pub.gpg | sudo apt-key add
Get the Sublime Text repository.
sudo add-apt-repository "deb https://download.sublimetext.com/ apt/stable/"
Now install Sublime text from the Ubuntu repositories.
sudo apt install sublime-text
We can find Sublime Text in the Applications or search it using the Activities menu.
Step-4: Develop a simple Node.js application using Sublime Text IDE
In this section, you will learn how to use Node.js with Sublime Text IDE by building a simple web server.
Let's do this!
4.1 Create a project directory and open it with Sublime Text
Make the project directory and script file.
$ mkdir nodeServer
$ touch nodeServer/index.js
Now open the project in Sublime Text.
subl nodeServer
4.2 Run Node.js in Sublime Text
Access the Command Palette using the shortcut: ctrl+shift+p.
Search Package Control Install Package by typing: pkgctl.
We get a confirmation message.
Click OK, then reopen the Command Palette and search Install Package.
Search for Nodejs and click on it to install it on Sublime Text.
Lastly, modify the Node.js run command by visiting Preferences -> Package Settings -> Nodejs -> Settings - User
and changing the value of "node_command": false
to "node_command": "/usr/local/bin/node"
.
Now we can build a Node.js program from Sublime Text using the shortcut: ctrl+b. First, let's create the web server.
4.3 Create and run a web server on Sublime Text
Update the index.js file with the following content.
const { createServer } = require("http")
const server = createServer( (request, response) => {
if (request.url === "/")
response.end("Hello hello! Now you know how to use Node.js with sublime text IDE on Ubuntu 22.04")
})
const PORT = process.env.PORT || 8000
server.listen(PORT, console.log(`The server runs on http://localhost:${PORT}`))
We import the http
module's createServer()
function and use it to create the web server, which listens for requests on port 8000.
On running the script file (ctrl+b), the terminal interface echoes The server runs on http://localhost:8000.
We can visit the /
URL and see whether the page reads Hello hello! Now you know how to use Node.js with sublime text IDE on Ubuntu 22.04, as we have specified in the code.
Copy and run the URL on any browser's search bar.
And voila, we get the expected response!
Conclusion
This tutorial taught you how to use Node.js with sublime text IDE. You installed Node.js and Sublime Text 3 on Ubuntu 22.04. Then configured Sublime Text to run Node.js without leaving the IDE. Lastly, you built a web server on the IDE.
It is your turn to practice the Sublime shortcuts and enjoy building Node.js applications on the IDE.