[英]Creation module through package.json
I very badly understood how I can develop my new modules. 我非常了解如何开发新模块。
In node_modules\\npm\\node_modules I create folder myx with 2 files package.json 在node_modules \\ npm \\ node_modules中,我创建了包含2个文件package.json的文件夹myx
{
"name" : "some",
"main" : "lib.js"
}
lib.js lib.js
console.log('lib');
I run server 我运行服务器
require('myx');
Error: Cannot find module 'myx' 错误:找不到模块'myx'
You should never manually edit anything inside a node_modules folder. 您永远不应手动编辑node_modules文件夹中的任何内容。 That folder is managed by the npm package manager, and it may overwrite your changes.
该文件夹由npm包管理器管理,它可能会覆盖您的更改。
If you just want to require
a file, just put the file in your app's folder and use a qualified filesystem path like require('./myx')
. 如果你只是想
require
一个文件,只需将文件放在您的应用程序的文件夹,并像使用合格的文件系统路径require('./myx')
If you want to create a new module which you will use in multiple projects and/or publish to the public npm registry: 如果要创建一个新模块 ,您将在多个项目中使用和/或发布到公共npm注册表:
npm init
in that folder. npm init
。 It will help you create a package.json file. You can now require
your module from other projects by using a fully-qualified path. 现在,您可以
require
使用完全合格的路径从其他项目的模块。 For example, 例如,
require('/home/me/myx');
Node uses its file loading rules to determine exactly which js file from your myx folder will be loaded. Node使用其文件加载规则来确定将加载myx文件夹中的哪个js文件。
Loading the module this way is fine for development and testing, but you probably want to install the module into your projects (just like other modules). 以这种方式加载模块适用于开发和测试,但您可能希望将模块安装到项目中(就像其他模块一样)。
Fortunately, npm supports installing from sources other than the registry . 幸运的是,npm支持从注册表以外的源安装 。 In your project folder, run:
在项目文件夹中,运行:
npm install /home/me/myx
npm will install the module into your project's node_modules, which means you can just npm会将模块安装到项目的node_modules中,这意味着你可以
require('myx');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.