Project Configuration

Eventually little scripts grow up. They start to span multiple files. They depend on external libraries. They become polished to the point that other developers want to use them. To prepare for this day, you will probably need to help your little script become a Node.js project.

Every Node.js project sits in its own directory and has a special configuration file named package.json, which has this general structure:

{
"name": "merge-json",
"description": "This utility combines two JSON files.",
"version": "1.0.0",
"main": "main.js"
}

There's a lot of extra metadata you can include, like the project's website, repository URL, and license. Nearly all of this metadata can be left out at this point in your learning. This metadata is consumed by the npm (Node Package Manager) utility that is installed alongside the node interpreter.

A scripts object in this structure provides a list of shortcuts that expand to terminal commands. Many projects, for example, have a start command that runs the main script and a test command that runs unit tests:

{
"name": "merge-json",
"description": "This utility combines two JSON files.",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"start": "node index.js",
"test": "jest test"
}
}

To invoke a script, you execute npm run SCRIPT-NAME in your terminal. To run your application, you execute npm run start. Interestingly, some typos are recognized. The command npm urn start works just as well.

The package.json configuration may be written by hand or generated interactively by running npm init.

Try creating a new directory, moving to it your terminal, adding a script, and creating your own package.json using npm init. Does npm run start execute your script?