如何将本地模块指定为npm软件包依赖项 [英] how to specify local modules as npm package dependencies

查看:357
本文介绍了如何将本地模块指定为npm软件包依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,该应用程序对package.json文件中指定的第三方模块(例如'express')具有通常的依赖关系.例如

I have an application which has the usual set of dependencies on third party modules (e.g. 'express') specified in the package.json file under dependencies. E.g.

"express"     : "3.1.1"

我想模块化地构造自己的代码,并由package.json安装一组本地(即我当前所在的文件系统上的)模块.我知道我可以通过运行以下命令来安装本地模块:

I would like to structure my own code modularly and have a set of local (meaning on the file system i am currently in) modules be installed by the package.json. I know that i can install a local module by running:

npm install path/to/mymodule

但是,我不知道如何通过package.json依赖项结构来实现这一点.在此命令中使用--save选项只是将"mymodule": "0.0.0"放入我的package.json中(不引用文件路径位置).如果我随后从node_modules中删除已安装的版本,并尝试从package.json重新安装,它将失败(因为它在中央注册表中查找"mymodule",并且不在本地查找).

However, I don't know how to make this happen via the package.json dependencies structure. Using the --save option in this command is simply putting "mymodule": "0.0.0" into my package.json (doesn't reference the filepath location). If i then remove the installed version from node_modules, and try to re-install from the package.json, it fails (because it looks for "mymodule" in the central registry, and doesn't look locally).

我确定这是一种告诉"dependencies": {}结构要从文件系统路径安装它的方法,但是不知道如何.

I'm sure the is a way of telling the "dependencies": {} structure that I want it to be installed from a file system path, but don't know how.

还有其他人有这个问题吗? 谢谢.

Anyone else had this problem? Thanks.

推荐答案

简短答案:

npm install --save ../my-local-repo

注意:确保将本地项目实际配置为模块.请参见创建NodeJS模块.

Note: make sure your local project is actually configured as a module. See Creating NodeJS modules.

从NPM 2.0.0开始,本地支持导入本地依赖项.这是danilopopeye记录的记录的.我已将其回复复制到此处,以帮助需要查找正确答案的任何人,因为该问题在Google的搜索结果中排名很高.

As of NPM 2.0.0, importing local dependencies is supported natively. This was documented by danilopopeye in response to a similar question. I've copied his response here to help anyone needing to find the correct answer, as this question ranks very highly in Google's search results.

此功能已在npm的2.0.0版本中实现.例如:

This feature was implemented in the version 2.0.0 of npm. For example:

{
  "name": "baz",
  "dependencies": {
    "bar": "file:../foo/bar"
  }
}

以下任何路径均有效:

../foo/bar
~/foo/bar
./foo/bar
/foo/bar

这篇关于如何将本地模块指定为npm软件包依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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