Package scripts
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 run the start
script to start your bot or the lint
script to lint your code for errors.
npm run start
npm run lint
Getting started
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 the following command in the console to generate one.
npm init -y
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"
}
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
We'll assume you have finished the creating your app 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
:
{
"name": "my-bot",
"version": "1.0.0",
"description": "A Discord bot!",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1" // needs a comma
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node ."
},
"keywords": [],
"author": "",
"license": "ISC"
}
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 start
script in your bot's directory, it will run the node .
command.
npm run start
Let's create another script to lint your code via the command line. Add the following line to your scripts:
{
"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 ." // needs a comma
"start": "node .",
"lint": "eslint ."
},
"keywords": [],
"author": "",
"license": "ISC"
}
Now, whenever you run the lint
script, ESLint will lint your index.js
file.
npm run lint
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"
}
And that's it! You can always add more scripts now, running them with:
npm run <script-name>