node.js NPM:找不到“npm link”模块后
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24550515/
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
NPM: After "npm link" module is not found
提问by Roc
I'm developing two modules for NodeJS, first one named aligatorand second one aligator-methods. Second one depends on first one to work. I'm developing these two modules at the same time and I want to global link aligatorso I can use it like it is on npm registry and I just installed it globally. To do this NPM documentation says that I need to use npm linkbut it's not working.
我正在为 NodeJS 开发两个模块,第一个命名为aligator,第二个为aligator-methods。第二个取决于第一个工作。我正在同时开发这两个模块,我想要全局链接,aligator这样我就可以像在 npm registry 上一样使用它,并且我只是在全局安装了它。要做到这一点,NPM 文档说我需要使用npm link但它不起作用。
File package.jsonof module aligator:
package.json模块文件aligator:
{
"name": "aligator",
"version": "0.0.1",
"description": "",
"main": "index.js",
"private": true,
"directories": {
"doc": "docs",
"example": "examples",
"test": "spec"
},
"scripts": {
"test": "gulp jasmine"
},
"license": "MIT",
"devDependencies": {
"gulp": "^3.6.2",
"gulp-jasmine": "^0.2.0",
"gulp-jshint": "^1.6.1",
"gulp-rename": "^1.2.0",
"jasmine-node": "^1.14.3"
},
"dependencies": {
"bluebird": "^1.2.4",
"lodash": "^2.4.1",
"mathjs": "^0.22.0"
}
}
File package.jsonof module aligator-methods:
package.json模块文件aligator-methods:
{
"name": "aligator-methods",
"version": "0.0.1",
"description": "",
"main": "index.js",
"private": true,
"directories": {
"doc": "docs",
"example": "examples",
"test": "jasmine"
},
"scripts": {
"test": "gulp jasmine"
},
"author": "",
"license": "MIT",
"devDependencies": {
"gulp": "^3.6.2",
"gulp-jasmine": "^0.2.0",
"gulp-jshint": "^1.6.1",
"gulp-rename": "^1.2.0",
"jasmine-node": "^1.14.3"
},
"dependencies": {
"lodash": "^2.4.1",
"mathjs": "^0.22.0",
"aligator": "^0.0.1"
}
}
First of all I linked the module globally:
首先,我全局链接了模块:
$ cd ~/aligator
$ npm link
/usr/local/lib/node_modules/aligator -> /Users/roc/aligator
This if I'm not mistaken has created a global reference of my module aligatorand now I can use this module from everywhere I want in the computer.
如果我没记错的话,这已经为我的模块创建了一个全局引用,aligator现在我可以在计算机中的任何地方使用这个模块。
Then I went to the other module and tried to install the dependency but it gave me this output:
然后我转到另一个模块并尝试安装依赖项,但它给了我这个输出:
$ cd ~/aligator-methods
$ npm install
npm ERR! 404 404 Not Found: aligator
npm ERR! 404
npm ERR! 404 'aligator' is not in the npm registry.
npm ERR! 404 You should bug the author to publish it
npm ERR! 404 It was specified as a dependency of 'aligator-methods'
npm ERR! 404
npm ERR! 404 Note that you can also install from a
npm ERR! 404 tarball, folder, or http url, or git url.
npm ERR! System Darwin 13.2.0
npm ERR! command "node" "/usr/local/bin/npm" "install"
npm ERR! cwd /Users/roc/aligator-methods
npm ERR! node -v v0.10.28
npm ERR! npm -v 1.4.16
npm ERR! code E404
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR! /Users/roc/aligator-methods/npm-debug.log
npm ERR! not ok code 0
I even tried to link it directly with:
我什至试图将它直接链接到:
$ cd ~/aligator-methods
$ npm link aligator
/Users/roc/aligator-methods/node_modules/aligator -> /usr/local/lib/node_modules/aligator -> /Users/roc/aligator
But it didn't work either.
但它也没有奏效。
Any thoughts on what it is that could be happening?I read somewhere that maybe it had something to do with my installation of nodeand npmbecause it was made by Homebrew and so sometimes I need to use sudo, it seemed unlikely but I tried what they proposed and It didn't work either.
关于可能发生的事情的任何想法?我在某处读到可能与我安装node和npm 有关系,因为它是由 Homebrew 制作的,所以有时我需要使用sudo,这似乎不太可能,但我尝试了他们提出的建议,但也没有用。
采纳答案by Roc
The problem was that the mainproperty of package.jsonwas pointing to a non-existing file. It seems that the problem can happen due to multiple reasons so be sure to take a look at other answers.
问题是 的main属性package.json指向一个不存在的文件。似乎问题可能由于多种原因而发生,因此请务必查看其他答案。
回答by linuxdan
I ran into this issue because of NVM, I was running one version of node for the dependency and another for the dependant.
由于 NVM,我遇到了这个问题,我正在为依赖项运行一个版本的节点,为依赖项运行另一个版本。
回答by Sean Inge Asbj?rnsen
Deleting package-lock.jsonthen running npm installagain resolved the issue for me.
删除package-lock.json然后npm install再次运行为我解决了这个问题。
回答by dylants
When you first run npm linkfrom the aligatordirectory, you create a link from your global node_modules directory to aligator. Then when you run the npm link aligatorfrom the aligator-methodsdirectory, you link aligatorfrom your locally installed node_modules to the original source (as the output shows in your example above). Once this is done, there shouldn't be a need to install anymore since it's already "installed". What errors are you seeing after you run the npm link aligatorcommand?
当您第一次npm link从该aligator目录运行时,您会创建一个从全局 node_modules 目录到aligator. 然后,当您npm link aligator从aligator-methods目录运行 时,您aligator将从本地安装的 node_modules链接到原始源(如上面示例中的输出所示)。一旦完成,就不需要再安装了,因为它已经“安装”了。运行npm link aligator命令后您看到什么错误?
If you just want to install a dependency from a local directory, you might just try using npm installinstead. For example:
如果您只想从本地目录安装依赖项,您可以尝试使用npm install。例如:
$ cd ~/aligator-methods
$ npm install ../aligator
$ cd ~/aligator-methods
$ npm install ../aligator
回答by FrostyDog
My issue ended up being that repo A was using npmand repo B was using yarn, so I needed to run yarn linkin repo B in order to pull it in via npm link package-nameinto repo A.
我的问题最终是 repo A 正在使用npm而 repo B 正在使用yarn,所以我需要yarn link在 repo B 中运行以便通过将其拉npm link package-name入 repo A。
回答by SandeepJ
What worked for me was to:
对我有用的是:
- Delete the
node_modulesin both the dependency and the consumer module. - Run
npm unlink --no-save [dependency-module] - re-link with the 2-link commands as per npm-link
Now I am able to fully test my unpublished module locally.
现在我可以在本地完全测试我未发布的模块。
Additionally, there is an npm pack command which can help you test your unpublished modules, although not quite as robust.
此外,还有一个 npm pack 命令可以帮助您测试未发布的模块,尽管没有那么强大。
回答by Flion
For me this happened when I decreased the version numberof my local package from 0.1.0 to 0.0.1. And in the projects where I linked to this package I was still using the higher version number. Updating dependencies in package.jsonfixed it.
对我来说,这发生在我将本地包的版本号从 0.1.0 减少到 0.0.1 时。在我链接到这个包的项目中,我仍然使用更高的版本号。更新依赖项package.json修复了它。
回答by crollywood
Fix for my version of this issue; in npm v5.3.0, I removed node_modulesfrom repo I was linking into another project.
修复我这个问题的版本;在 npm v5.3.0 中,我node_modules从正在链接到另一个项目的 repo 中删除。
I found out that after npm v3 they try to put all node_modules dependencies into one node_modules directory (one in your project) to flatten the structure as much as possible (http://codetunnel.io/npm-5-changes-to-npm-link/).
我发现在 npm v3 之后,他们尝试将所有 node_modules 依赖项放入一个 node_modules 目录(在您的项目中)以尽可能地扁平化结构(http://codetunnel.io/npm-5-changes-to-npm -链接/)。

