简体   繁体   English

通过package.json创建模块

[英]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注册表:

  1. Create a new folder for your module – for example myx . 为您的模块创建一个新文件夹 - 例如myx
  2. Run npm init in that folder. 在该文件夹中运行npm init It will help you create a package.json file. 它将帮助您创建package.json文件。
  3. Create your js files. 创建您的js文件。

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.

相关问题 通过ng new命令创建Angular新项目正在使用旧版本创建package.json - Angular new project creation through ng new command is creating package.json with old versions 什么是“module”package.json字段? - What is the “module” package.json field for? 电子找不到模块 package.json - electron cannot find module package.json 用于匹配 package.json 内容中的模块的正则表达式 - Regular expression to match a module in a package.json content 为生产而构建会导致模块package.json中的错误 - Building for production causes errors in module package.json NPM模块-如何利用package.json文件? - NPM Module - How to leverage the package.json file? `main` 和 `module` 与 package.json 中的 `exports` 有什么区别? - What is the difference between `main` and `module` vs `exports` in package.json? 在 package.json 中将“类型”设置为“模块”然后无法读取 es 模块 - setting "type" to "module" in package.json then not able to read es modules 无法在 React 中使用 package.json“导出”导入模块 - Can't import module using package.json "exports" in React .min或src作为node_module的package.json中的“main”-file - .min or src as “main”-file in package.json for node_module
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM