Building Enterprise JavaScript Applications
上QQ阅读APP看书,第一时间看更新

Starting projects with npm

For Node.js projects, the settings and configurations are stored inside a file named package.json, located at the root of the repository. The npm CLI tool provides a npm init command, which will initiate a mini-wizard that helps you compose your package.json file. So, inside our project directory, run npm init to initiate the wizard. 

The wizard will ask you a series of questions, but also provides sensible defaults. Let's go through each question one by one:

  1. package name: We are happy with the default name of hobnob (derived from the directory name), so we can just press the Return key to continue.
  2. version: We're going to follow semantic versioning (semver) here and use major version 0(0.y.z) to indicate that our code base is under initial development, and that the API is not stable. Semver also recommends that our initial release be 0.1.0.
  1. description: A brief description of your project; if we make our application public on npmjs.com, this description will appear in the search results.
  2. entry point: This should point to the root of the module, and is what is run when other modules require your module. We have not decided on the structure of our application yet, so just leave it at index.js, and we may change it later.
  3. test command: This will be run when we run npm run test. We will integrate with the Cucumber and Mocha testing frameworks later; for now, just leave it blank.
  4. git repository: Use the remote repository we created earlier, for example, git@github.com:d4nyll/hobnob.git.
  5. keywords: These are comma-separated keywords that help others search for your package on npmjs.com.
  6. author: Put your details here in the format of FirstName LastName <e@ma.il> (http://web.site/).
  7. license: The license tells others how they can use our code. It should be one of the identifiers in the SPDX License List (https://spdx.org/licenses/). For example, the MIT License would be MIT, and the GNU General Public License v3.0 would be GPL-3.0.
There are two  main  types of open source licenses permissive licenses focus on allowing others to do whatever they want with your code; while copyleft licenses promote sharing and require the sharing of derivative code under the same terms.  If you're unsure which license to choose, check out  choosealicense.com .

After you've completed the wizard, it'll show you a preview of the package.json file; press the Return key to confirm. You can also take a look at the newly-created package.json file to check:

$ cat package.json 
{
"name": "hobnob",
"version": "0.1.0",
"description": "Back end for a simple user directory API with
recommendation engine",

"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/d4nyll/hobnob.git"
},
"author": "Daniel Li <dan@danyll.com>",
"license": "MIT",
"bugs": {
"url": "https://github.com/d4nyll/hobnob/issues"
},
"homepage": "https://github.com/d4nyll/hobnob#readme"
}

The package.json file contains information about your project, as well as a list of packages that your project depends on. Having a package.json file, it allows collaborators to quickly set up the project on their local environment—all they have to do is run npm install, and all the project's dependencies will be installed.