node.js 如何将本地模块指定为 npm 包依赖项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15806241/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 14:06:23  来源:igfitidea点击:

how to specify local modules as npm package dependencies

node.jsnpm

提问by Sam Adams

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.

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

"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:

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

npm install path/to/mymodule

However, I don't know how to make this happen via the package.json dependencies structure. Using the --saveoption 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).

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

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.

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

Anyone else had this problem? Thanks.

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

回答by Randy the Dev

npm installnow supports this

npm install现在支持这个

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

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

为此,mymodule必须将其配置为具有自己的package.json. 请参阅创建 NodeJS 模块

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.

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

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

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

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

以下任何路径也有效:

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

syncing updates

同步更新

Since npm installcopies mymoduleinto node_modules, changes in mymodule's source will not automatically be seen by the dependent project.

由于npm install复制mymodulenode_modulesmymodule的源中的更改将不会自动被依赖项目看到。

There are two ways to update the dependent project with

有两种方法可以更新依赖项目

  • Update the version of mymoduleand then use npm update: As you can see above, the package.json"dependencies" entry does not include a version specifier as you would see for normal dependencies. Instead, for local dependencies, npm updatejust tries to make sure the latestversion is installed, as determined by mymodule's package.json. See chriskelly's answer to this specific problem.

  • Reinstall using npm install.This will install whatever is at mymodule's source path, even if it is older, or has an alternate branch checked out, whatever.

  • 更新版本mymodule然后使用npm update: 正如您在上面看到的,package.json“依赖项”条目不包含您在普通依赖项中看到的版本说明符。相反,对于本地依赖项,npm update只需尝试确保安装了最新版本,由mymodule's确定package.json。请参阅chriskelly 对此特定问题的回答

  • 使用npm install. 这将安装 atmymodule的源路径中的任何内容,即使它较旧,或者已检出备用分支,等等。

回答by jasoncrawford

See: Local dependency in package.json

请参阅:package.json 中的本地依赖项

It looks like the answer is npm link: https://docs.npmjs.com/cli/link

看起来答案是npm linkhttps: //docs.npmjs.com/cli/link

回答by Sam Adams

I couldn't find a neat way in the end so I went for create a directory called local_modulesand then added this bashscript to the package.json in scripts->preinstall

我最终找不到一个简洁的方法,所以我创建了一个名为的目录local_modules,然后将此 bashscript 添加到 script->preinstall 中的 package.json

#!/bin/sh
for i in $(find ./local_modules -type d -maxdepth 1) ; do
    packageJson="${i}/package.json"
    if [ -f "${packageJson}" ]; then
        echo "installing ${i}..."
        npm install "${i}"
    fi
done

回答by Anurag Dutta

After struggling much with the npm linkcommand (suggested solution for developing local modules without publishing them to a registry or maintaining a separate copy in the node_modules folder), I built a small npm module to help with this issue.

在使用该npm link命令(无需将它们发布到注册表或在 node_modules 文件夹中维护单独副本的情况下开发本地模块的建议解决方案)苦苦挣扎之后,我构建了一个小的 npm 模块来帮助解决这个问题。

The fix requires two easy steps.

修复需要两个简单的步骤

First:

第一的:

npm install lib-manager --save-dev

Second, add this to your package.json:

其次,将此添加到您的package.json

{  
  "name": "yourModuleName",  
  // ...
  "scripts": {
    "postinstall": "./node_modules/.bin/local-link"
  }
}

More details at https://www.npmjs.com/package/lib-manager. Hope it helps someone.

更多详细信息,访问https://www.npmjs.com/package/lib-manager。希望它可以帮助某人。

回答by Plato

If it's acceptible to simply publish your modules preinstalled in node_modules alongside your other files, you can do it like this:

如果可以简单地将预装在 node_modules 中的模块与其他文件一起发布,您可以这样做:

// ./node_modules/foo/package.json
{ 
  "name":"foo",
  "version":"0.0.1",
  "main":"index.js"
}

// ./package.json
...
"dependencies": {
  "foo":"0.0.1",
  "bar":"*"
}

// ./app.js
var foo = require('foo');

You may also want to store your module on git and tell your parent package.json to install the dependency from git: https://npmjs.org/doc/json.html#Git-URLs-as-Dependencies

您可能还想将您的模块存储在 git 上并告诉您的父 package.json 从 git 安装依赖项:https: //npmjs.org/doc/json.html#Git-URLs-as-Dependencies