Node Js Create Slug From String or Titles using Slugify Tutorial

We all are very much familiar with slugs, and it is the exact text with dashes that resides in the browser’s address bar.

But how do you generate slug in Node?

In this short and simple tutorial, we will explain how to create Slugs from simple paragraph text and titles in the Node application.

To generate slugs from the title, we will use the Slugify library. This might be a simple feature, but very much significant, and knowing small features enhance profoundness in your programming sense.

Slugify is a small yet prevalent library, and it is mainly used to create slugs in node-based apps. It offers some handy options to clean your ugly titles and convert them to slugs.

To use the slugify in the node app, you first have to install it, then call the slugify object by importing it. Slugify object takes two arguments, the first argument is the string of title, and the second argument is the options that sanitize the title.

How to Easily Convert Titles into Slug in Node with Slugify

  • Step 1: Build Project Folder
  • Step 2: Create Package.json
  • Step 3: Register App.js in Scripts
  • Step 4: Install Slugify Package
  • Step 5: Create Slug in Node
  • Step 6: Run Node App

Build Project Folder

We assume, you are starting from absolute scratch. Let us first set up the environment for development in node.

Open the terminal window, on the console type the command. This command is for generating the project directory.

FYI, you can name it whatever you like.

mkdir node-demo

Once the folder is ready, go inside the project folder.

cd node-demo

Create Package.json

A node project requires a specific package.json file in its root. In this file all the additional information about the scripts, packages stored.

Execute command from your console to generate the package.json file.

npm init

Now, you can open and check the metadata of your node project.

Register App.js in Scripts

Now, we need a main project file, in your project directory, create a new file name it app.js,

We will be showing output on terminal’s console, for that we will be executing node command with reference to the main app.js file.

Ensure that you have added the file name in scripts segment.

Open package.json file and insert the following code.

  "scripts": {
    "start": "node app.js"

Install Slugify Package

Open source is a loyal friend of developers; you name it, any feature open-source always has the solution for specific functionality.

Similarly, in this guide, we will be using the slugify package that helps convert titles into slugs.

Let’s run the command to install the slugify package.

npm install slugify

Generate Slug from String Title using Slugify

Now plugins has been added to node directory.

Next, import the slugify module this allow you to create multiple instances from slugify object.

Instances means the titles or string that we convert to slug.

We are using the three methods, where first method is simple. We are not passing any option and simply defining a paragraph with some simple text and transforming into slug.

What if your title contains special characters or camel case characters.

This can be dealt with options property; the options is an object that holds specific properties that helps in sanitizing the title and convert perfectly to slugs.

const slugify = require('slugify')

// Node Convert Title to Slug Example
const myTitle1 =
  'The only way to learn a new programming language is by writing programs'
const newSlug1 = slugify(myTitle1)

// Slugify config options
const options = {
  replacement: '-',
  remove: undefined,
  lower: true,
  strict: false,
  locale: 'en',
  trim: true,

const myTitle2 =
  'Sometimes it is better to leave something alone, to pause, and that is very true of programming'
const newSlug2 = slugify(myTitle2, options)

const myTitle3 = 'Just keep your cool and your sense of humor'
const newSlug3 = slugify(myTitle3, options)

If you want to know more about what every options property does, please checkout the following code example.

slugify('title string', {
  replacement: '-',  // replace spaces with replacement character, defaults to `-`
  remove: undefined, // remove characters that match regex, defaults to `undefined`
  lower: false,      // convert to lower case, defaults to `false`
  strict: false,     // strip special characters except replacement, defaults to `false`
  locale: 'vi',       // language code of the locale to use
  trim: true         // trim leading and trailing replacement chars, defaults to `true`

Run Node App

If you want to test out what we have taught you, you can directly open your console.

Type the given command then run to check the title to slug conversion.

node app.js

On your terminal window, you will see the given output for your strings.

# The-only-way-to-learn-a-new-programming-language-is-by-writing-programs

# sometimes-it-is-better-to-leave-something-alone-to-pause-and-that-is-very-true-of-programming

# just-keep-your-cool-and-your-sense-of-humor


Node Js Create Slug From String or Titles using Slugify Tutorial

So this was it; up until now, we have uncovered the process of making slugs from titles in the node js application. We started from scratch and gave you an overview of how to get started with node.

Most importantly, explained how to use Slugify in node to form new slugs from title strings. We also learned how to use the slugify options to beautify the slug in the node.