2

How to setup npm package.json?

What are the following ? : "main": "index.js", "devDependencies", "scripts":

I try:

{
  "name": "progectapi2", //Name project
  "version": "1.0.0",    //Version project
  "description": "test", //description project
  "main": "index.js",    //What is it? 
  "dependencies": {      //dependencies package project
    "sass": "^0.5.0"
  },
  "devDependencies": {}, //What is it? 
  "scripts": {           //What is it? 
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "My_Name",   //Name autor
  "license": "UNLICENSED"//license project
}
teobais
  • 2,820
  • 1
  • 24
  • 36
Niko_D
  • 2,787
  • 2
  • 10
  • 7

2 Answers2

3

Full documentation for the package.json file can be found here but to answer your specific questions

  • main is the entry point to your library so when someone does a require('progectapi2') this should be the first file which is loaded.
  • devDependencies is a list of packages only required for developing your package, not for consuming or running it.
  • scripts are used adding commands which are run in response to certain user actions such as npm start
Andrew
  • 698
  • 5
  • 13
2

A generic definition of package.json could be:

All npm packages contain a file, usually in the project root, called package.json - this file holds various metadata relevant to the project. This file is used to give information to npm that allows it to identify the project as well as handle the project's dependencies. It can also contain other metadata such as a project description, the version of the project in a particular distribution, license information, even configuration data - all of which can be vital to both npm and to the end users of the package. The package.json file is normally located at the root directory of a Node.js project.

Running npm init on the working directory of the project that you want to distribute as an npm package, will guide you, through a command-line wizard, to creating the package.json file, through some questions (i.e. description of your project, contributors, etc.).

dependencies are other projects, required to run the specified project as an end user, whereas devDependencies, are the ones required to also develop the project. Here is a more detailed answer, according to all kinds of dependencies.

main
The main field is a module ID that is the primary entry point to your program. That is, if your package is named foo, and a user installs it, and then does require("foo"), then your main module's exports object will be returned.

scripts
You can think of scripts as an object which exposes additional npm commands. The object assumes that the key is the npm command and the value is the script path.
For instance, according to your case, when running npm test the console will display Error: no test specified and will then exit.

For more information, you can read the full documentation and/or have a look at this interactive guide.

Community
  • 1
  • 1
teobais
  • 2,820
  • 1
  • 24
  • 36