Setting up package.json scripts

An easy way to run scripts like a script to start your bot, a script to lint your bot's files, or whatever scripts you use is by storing them in your package.json file. After you store these scripts in your package.json file, you can type npm run start to start your bot or npm run lint to lint your code for errors.

Getting started

TIP

Before getting started, you'll need to have a package.json file. If you don't have a package.json file yet, you can run npm init -y in the console to generate one.

If you haven't touched your package.json file yet (excluding installing dependencies), your package.json file should look similar to the following:

{
	"name": "my-bot",
	"version": "1.0.0",
	"description": "A Discord bot!",
	"main": "index.js",
	"scripts": {
		"test": "echo \"Error: no test specified\" && exit 1"
	},
	"keywords": [],
	"author": "",
	"license": "ISC"
}
1
2
3
4
5
6
7
8
9
10
11
12

Let's zoom in more. Below main, you'll see scripts. You can specify your scripts there. In this guide, we'll show how to start and lint your bot using a package.json script.

Adding your first script

TIP

We'll assume you have finished the creating your first bot section of the guide. If you haven't, ensure to follow it first!

Over at your package.json file, add the following line to the scripts:

"start": "node ."
1

TIP

The node . script will run the file you have specified at the main entry in your package.json file. If you don't have it set yet, make sure to select your bot's main file as main!

Now, whenever you run the npm run start script in your bot's directory, it will run the node . command. Let's create another script to lint your code via the command line.

TIP

If you do not have ESLint installed globally, you can use npxopen in new window to run the ESLint script for your local directory. For more info on how to set it up, you can read the site hereopen in new window.

Add the following line to your scripts:

"lint": "eslint ."
1

Now, whenever you run the npm run lint script, ESLint will lint your index.js file.

Your package.json file should now look similar to the following:

{
	"name": "my-bot",
	"version": "1.0.0",
	"description": "A Discord bot!",
	"main": "index.js",
	"scripts": {
		"test": "echo \"Error: no test specified\" && exit 1",
		"start": "node .",
		"lint": "eslint ."
	},
	"keywords": [],
	"author": "",
	"license": "ISC"
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14

And that's it! You can always add more scripts now, running them with npm run script-name.