node. JS Getting Started: Package structure

Source: Internet
Author: User

JavaScript lacks a package structure. COMMONJS is committed to changing the status quo, thus defining the package's Structural specification (http://wiki.commonjs.org/wiki/Packages/1.0). The advent of NPM is to solve the problem of package loading, management and versioning, based on the COMMONJS specification.    Require the search mechanism is clear, let's look at the details of the package. A package that conforms to the COMMONJS specification should be the following structure:
    • A Package.json file should exist in the top-level directory of the package
    • The binaries should be included in the Bin directory.
    • The JavaScript code should be included in the Lib directory.
    • The document should be in the doc directory.
    • The unit test should be in the test directory.
node. JS attempts to load the current directory as a package when no target file is found, so the most important field in the Package.json file is main. In fact, this is an extension of node. js, which does not include this field in the standard definition, and for require, only the main property is required. However, in addition to the package needs to accept the installation, uninstallation, dependency management, version management and other processes, so commonjs for the Package.json file defines the following required fields:
    • Name: The package name, which needs to be unique on NPM. Cannot contain spaces.
    • Description: Package Introduction. It is usually displayed in some lists.
    • Version: Revision number. A semantic version number (http://semver.org/), usually x.y.z. This version number is important and is often used in some version-control scenarios.
    • Keywords: key word group. Used for categorical searches in NPM.
    • Maintainers: An array of package maintainers. An array element is a JSON object that contains the name, email, and web three properties.
    • Contributors: An array of package contributors. The first one is the author of the package. In the open source community, if the patch being submitted is being merge into the master branch, it should be added to the person who contributed the patch. The format contains name and email.
    • Bugs: A URL address that can submit a bug. Can be an e-mail address (Mailto:[email protected]) or a web address (http://url).
    • Licenses: The license used by the package.
    • Repositories: An array of addresses for the managed source code.
    • Dependencies: The dependency required for the current package. This property is important, and NPM will help you automatically load dependent packages with this property.
The following is the express framework of the Package.json file, worth reference.
1 "name": "Express"2 "description": "Sinatra inspired Web development framework"  3 "version": "3.0.0alpha1-pre"4 "author": "TJ Holowaychuk",

In addition to some of the required fields mentioned earlier, we found additional fields such as bin, scripts, engines, Devdependencies, author. Here you can focus on the scripts field. The Package Manager (NPM) needs to do some compilation or cleanup when installing or uninstalling the package, and the Scripts Field object indicates which file to run when the operation is performed, or executes the fetch command. The following is a more comprehensive scripts case:

1 "Scripts"2     "Install": "Install.js"3     "Uninstall": " Uninstall.js "4     " "Build": "Build.js"5     "Doc": "Make-doc.js"   6     "test": "Test.js"7 }

If you have perfected your JavaScript library to implement the COMMONJS package specification, you can publish your own packages via NPM.

NPM Publish <folder>
The command is very simple. But before you do, you need to register an account on NPM with the NPM adduser command for subsequent package maintenance.    NPM parses the Package.json file under the folder and uploads the directory to the NPM site. Users are also very concise when using your package:
NPM Install <package>
Even for packages that NPM cannot install (because of some strange network reasons), you can manually download its stable version via GitHub and then install it with the following command:
NPM Install <package.json folder>
Simply point the path to the directory where the Package.json exists.    You can then use require (' package ') in your code. The complexity of the internal flow of require in node. js, and the simplicity of the method invocation is really worth the breath.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.