本地需要节点模块 [英] Requiring node modules locally

查看:25
本文介绍了本地需要节点模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用已经在 NPM 上注册的模块时,包含它们的过程很简单:运行 npm install 然后添加 var package = require(';')

When working with modules already registered on NPM, the process of including them is easy: run npm install <package> and then add var package = require('<package>')

但是,我不确定设置"的方式;在我自己的模块上工作时.我还没有准备好发布到 NPM,但我确实希望以与之前概述的相同的方式require 模块.

However, I'm not sure of the way to "set things up" when working on my own module. I'm not ready to publish to NPM but I do want to require the module in the same way as outlined before.

因此,我完成了以下步骤:

Therefore, I've completed the following steps:

  1. node_moduels 目录中为我的模块创建了一个子目录
  2. 在这个新目录中添加了一个 package.json 文件(通过 npm init)
  3. package.json 文件中包含一个 dependencies 部分
  1. Created a sub-directory inside the node_moduels directory for my module
  2. Added a package.json file (via npm init) inside this new directory
  3. Included a dependencies section in the package.json file

这是在本地使用节点模块的正确方法吗.

Is this the correct approach to using node modules locally.

此外,当我运行 npm install 时,我的模块的 package.json 文件中似乎没有检测到依赖项 - 我认为这是我处理事情的方式的问题吗?

Also, when I run npm install the dependencies do not appear to be detected in my module's package.json file - I assume this is an issue with the way I've gone about things?

推荐答案

我不建议将它放在 node_modules 目录中.此文件夹应从源代码管理中排除.

I would not suggest putting it in the node_modules directory. This folder should be excluded from your source control.

这是一个最小的端到端示例.

Here's a minimal end to end example.

把这个文件放在任何你喜欢的地方.我建议在您的目录结构中添加一个lib"文件夹

Put this file wherever you like. I suggest a 'lib' folder within your directory structure

myModule.js

module.exports = function(callback){
    return callback("hello there");
}; 

然后,无论您想在哪里使用它:

Then, wherever you want to use it:

app.js

var myModule = require('./lib/myModule');

myModule.sayHello(function(hello) {
    console.log(hello);
});

现在,如果您运行 node app.js,您的控制台输出将是:

Now, if you run node app.js your console output will be:

你好

随着您的 myModule 的增长,您可以将其重构为一组单独的文件,为其创建 package.json,并将其发布到 NPM

As your myModule grows, you can refactor this into a separate set of files, create an package.json for it, and publish it to NPM

编辑

根据您的评论,这似乎是您想要的

Based on your comment, it looks like this is what you want

package.json 中的本地依赖

因此,在此基础上,结合我们上面的示例,按如下方式编辑您的 package.json

So, based on that, along with our above example, edit your package.json as follows

{
  "dependencies": {
    "myModule": "file:../lib/myModule"
  }
}

然后你可以require为:

var myModule = require('myModule');

如果/当您将 myModule 发布到 npm,您只需更改您的 package.json

If / when you publish myModule to npm, you can just change your package.json

另一个编辑

作为另一种选择,您可以在 package.json 中指定 git urls,而无需发布到 NPM

As another alternative, you can specify git urls in your package.json without publishing to NPM

将 Git 依赖与 npm 和 Node on Heroku 结合使用

这篇关于本地需要节点模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆