If you are in OSX you can use
homebrew or install from a shell installer:
I’m using asdf so it’s
Run an example
Running it again it’s cached in your local directory, which defaults to
$HOME/.cache/deno. You can look in this directory to see what’s in there.
Installing a script
We can also install this “tool” locally to make it an executable.
deno install welcome https://deno.land/std/examples/welcome.ts
This pulls down all of the dependancies and installs an executable in
~/.deno/bin, which you may need to add to your path.
If you delete the
~/.cache/deno directory, the next time you run
welcome it will download all of the code again. This is fun because you can easily distribute and install scripts as needed and it takes care of pulling down all the dependacies that are needed.
Connecting to Redis
Lets see how to connect to redis. We will use the deno-redis package, by simply adding it as an
import statement at the top of the file. Then, when we run
deno run redis.ts it will download automatically without needing to use a seperate
We also will pull
REDIS_URL from the environment to let us control where things are deployed later. There’s a weird quirk with the way that
URL that reflects a weird way that browsers parse URLs, so we’re tweaking it a little bit to make sure that we can pull out the password and hostname if they are set.
The other thing we are doing is
redis object. We will use this later to build off of in another script.
The first time you run
deno run redis.ts it will download all of the needed components, and then it will give you the following error:
By default deno runs everything in a sandbox environment that has no access to the local environment. You need to specify what permissions this code – that gets pulled down from the internet on demand – has. We need to give it
--allow-env to see the environment (where you may have secured passwords stached) and, also in this case,
--allow-net because we want our script to be able to talk to redis.
…and we need to startup a redis server. If you have docker installed, the easiest way to do this (for a temporary test server) is:
Which means start a container based on the
--rm remove it when done,
-it give it an interactive terminal, and
-p 6379:6379 expose the local container port
6379 on the local network.
Once done, when we run our script we get:
Creating a webservice
This is the example that you see everywhere, except that
- We import
redisfrom our previous script
- And we call
redis.incrto add a counter for each “page”
Lets run this now with
deno run --allow-env --allow-net web.ts. With you browser go to http://localhost:8080/ and refresh to watch the counter go up and up. Exiciting!
Wrangling dependacies with
As scripts get bigger we want to be able to centralize which versions of what we are using. Instead of having a
package.json file where everything is listed, we can simply organize our project in a way that keeps it cleaner. The standard idiom is to have a
deps.js file that simply imports and exports things from one place. Lets create that now and update our source files to go through that.
web.ts change the imports to
redis.ts change the imports to
Now everything is centralized in one place. Running
Will make sure that everything in is the local cache, which we will take advantage of when building our Docker image.
Building a docker container for deployment
When we package up our application for deployment, we don’t want it to be pulling random source code from the internet when it first starts up. We’ll make that part of our build process. Here is a simple Dockerfile that
- Builds off of the alpine deno instance
- Copies the
deps.tsfile into the image
deno cache deps.tswhich pulls the require dependancies into the local cache
- Copies the rest of your source directory in
- And defines a startup command that includes the required permissions
Now from here you are ready to deploy this on whatever hosting provider you need. It needs a redis instance that it will find by looking at
REDIS_URL in the environment, which is the standard that
dokku (and probably others) use.
Lets test it out locally first. You’ll need to find your ip address which on linux you can do with
hostname -I. Change the
REDIS_URL to point to your local instance.
Now when you go to http://localhost:8080/ you should see deno running in your local container.
From here it will be straightforward to do a
git push heroku master or whatever to deploy on your provider of choice.
Deno is like a web browser for the command line. One thing that webbrowsers do amazingly well is the distrubtion of applications – just go to a URL and you have it on your machine.
deno install brings some of that magic into the command line. The ecosystem is still very young, but with ES modules being the extension mechanism that can pull code from wherever it lives on the internet you can imagine a much more distributed application mechanism that doesn’t rely on any particular centralized service.