See Details
目录
Node.js 模块是一种可以发布到 npm 的 包。
¥Node.js modules are a type of package that can be published to npm.
概述
¥Overview
创建 package.json
文件
¥Create a package.json
file
-
要创建
package.json
文件,请在命令行中,在 Node.js 模块的根目录中运行npm init
:¥To create a
package.json
file, on the command line, in the root directory of your Node.js module, runnpm init
:-
对于 范围模块,运行
npm init --scope=@scope-name
¥For scoped modules, run
npm init --scope=@scope-name
-
对于 无范围的模块,运行
npm init
¥For unscoped modules, run
npm init
-
-
为必填字段(
name
和version
)以及main
字段提供响应:¥Provide responses for the required fields (
name
andversion
), as well as themain
field:-
name
:你的模块的名称。¥
name
: The name of your module. -
version
:初始模块版本。我们建议遵循 语义版本控制指南 并从1.0.0
开始。¥
version
: The initial module version. We recommend following semantic versioning guidelines and starting with1.0.0
.
-
有关 package.json
文件的更多信息,请参阅“创建 package.json 文件”。
¥For more information on package.json
files, see "Creating a package.json file".
创建另一个应用需要你的模块时将加载的文件
¥Create the file that will be loaded when your module is required by another application
创建一个文件,其名称与你在 main
字段中提供的名称相同。在该文件中,将函数添加为 exports
对象的属性。这将使该函数可用于其他代码:
¥Create a file with the same name you provided in the main
field. In that file, add a function as a property of the exports
object. This will make the function available to other code:
exports.printMsg = function() {console.log("This is a message from the demo package");}