45
loading...
This website collects cookies to deliver better user experience
package.json
file is typically the first step in a Node.js project, and it has to be available if you want to add dependencies via npm in your project. There are many app scaffolding packages available, like create-react-app
, which do this for you. This blog post is about how to create a package.json
from scratch. Have a look at this article - What is the package.jsonnpm init
package.json
cd FOLDER_NAME
and run npm init
. You should see a prompt like this:
➜ test npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.
See `npm help init` for definitive documentation on these fields
and exactly what they do.
Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.
Press ^C at any time to quit.
package name: (test)
name
, version
, description
, main
, test command
, git repository
, keywords
, author
, and license
. It's okay to accept the defaults to get a package.json generated and be able to install dependencies. All fields are optional, if you don't publish your package to the NPM registry. If you do intend to publish your package, there are several requirements and in general you should fill out the prompts. You can learn more about the different fields about the package.json in this article - The Basics of Package.json. The only real requirement (for now) is that the package.json is valid JSON.package.json
after answering/accepting defaults of the prompts and accept (yes) or decline (no). The output should look something like this (accepted defaults):
{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
Is this OK? (yes)
package.json
and can now install dependencies for your project using npm install <package>
. You can always manually edit your package.json file in your editor, if you want to change some values.npm init -y
to generate a package.json and automatically accept all defaults. The generated file will be shown on the command line.package.json
with npm init
in the project directory.npm init -y
.