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

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

问题描述

我有一个应用程序,它对依赖项下的 package.json 文件中指定的第三方模块(例如express")具有通常的一组依赖项.例如

"express" : "3.1.1"

我想模块化地构建我自己的代码,并通过 package.json 安装一组本地(意思是我当前所在的文件系统)模块.我知道我可以通过运行安装本地模块:

npm install path/to/mymodule

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

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

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

解决方案

npm install 现在支持这个

npm install --save ../path/to/mymodule

要使其工作 mymodule 必须配置为具有自己的 package.json 的模块.请参阅创建 NodeJS 模块.

从 npm 2.0 开始,本机支持本地依赖项.请参阅danilopopeye 对类似问题的回答.我复制了他的回答,因为这个问题在网络搜索结果中排名非常高.

<块引用>

这个特性是在 npm 2.0.0 版本中实现的.例如:

<代码>{名称":baz",依赖关系":{bar":文件:../foo/bar";}}

<块引用>

以下任何路径也是有效的:

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

同步更新

由于 npm install 将目录中的包作为符号链接添加到当前项目中 对本地包的任何更改都是自动同步.

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"

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

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).

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 now supports this

npm install --save ../path/to/mymodule

For this to work mymodule must be configured as a module with its own package.json. See Creating NodeJS modules.

As of npm 2.0, local dependencies are supported natively. See danilopopeye's answer to a similar question. I've copied his response here as this question ranks very high in web search results.

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

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

Any of the following paths are also valid:

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

syncing updates

Since npm install <folder> adds the package in the directory as a symlink in the current project any changes to the local package are automatically synced.

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

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