[英]Private repo dependency installs but “Cannot Find Module”
I have a project that requires a private repo as a dependency. 我有一个项目,需要私人仓库作为依赖。 So, projectA has this included in the package.json as
"projectB": "user/repo"
. 因此,projectA将package.json包含在
"projectB": "user/repo"
。 This installs just fine, and is listed in projectA node_modules. 这安装得很好,并在projectA node_modules中列出。 The problem is, that node throws and error where I require the functions of the dependency.
问题是,该节点抛出错误,我需要依赖的功能。 The error being that
"Cannot find module projectB"
. 错误是
"Cannot find module projectB"
。 As mentioned, projectB is listed in node_modules. 如上所述,projectB在node_modules中列出。 Here is the structure of projectB:
这是projectB的结构:
.
├── README.md
├── file1.js
├── file2.js
├── file3.js
├── file4.js
└── package.json
It also has its own node_modules, but I've left that out. 它也有自己的node_modules,但我已经把它留了下来。 Now, here is what file1.js might look like:
现在,这是file1.js的样子:
function getResult (a, b) {
return a + b;
}
module.exports = { getResult }
And here is what projectA looks like: 这是projectA的样子:
var calculate = require('projectB').file1.getResult; // I've tried this in several other ways too
Calling calculate results in the "Cannot find module error"
. 调用计算结果为
"Cannot find module error"
。 Have I done something fundamentally wrong in setting up for using a private repo as dependency and/or requiring it wrong? 我是否在设置使用私有仓库作为依赖关系和/或要求错误时做了一些根本性的错误?
Update projectB package.json 更新projectB package.json
{
"name": "projectB",
"version": "1.0.0",
"description": "Backend utility functions",
"scripts": {
"test": "mocha"
},
"repository": {
"type": "git",
"url": "git+https://github.com/user/repo.git"
},
"author": "Me",
"license": "ISC",
"bugs": {
"url": "https://github.com//user/repo/issues"
},
"homepage": "https://github.com//user/repo#readme",
"dependencies": {
"lodash": "^4.17.4",
"mongodb": "^2.2.25",
"redis": "^2.7.1",
"winston": "^2.3.1"
}
}
projectB
needs to be updated to set an appropriate main
, but by default this will be index.js
. 需要更新
projectB
以设置适当的main
,但默认情况下这将是index.js
。 You could do something like the following: 您可以执行以下操作:
// projectB/index.js
exports.file1 = require("./file1");
exports.file2 = require("./file2");
exports.file3 = require("./file3");
exports.file4 = require("./file4");
It's actually a pretty common pattern to have an index.js
that does nothing but export from library files. 实际上,有一个
index.js
除了从库文件导出之外什么都不做,这实际上是一种非常常见的模式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.