Node.js 私人回购依赖项安装,但;找不到模块";
我有一个项目,需要一个私人回购作为一个依赖。因此,projectA将其作为Node.js 私人回购依赖项安装,但;找不到模块";,node.js,package.json,Node.js,Package.json,我有一个项目,需要一个私人回购作为一个依赖。因此,projectA将其作为“projectB”:“user/repo”包含在package.json中。这可以很好地安装,并在projectA node_模块中列出。问题是,节点在我需要依赖项函数的地方抛出错误。错误是“找不到模块projectB”。如上所述,projectB列在node_模块中。以下是projectB的结构: . ├── README.md ├── file1.js ├── file2.js ├── file3.js ├── fi
“projectB”:“user/repo”
包含在package.json中。这可以很好地安装,并在projectA node_模块中列出。问题是,节点在我需要依赖项函数的地方抛出错误。错误是“找不到模块projectB”
。如上所述,projectB列在node_模块中。以下是projectB的结构:
.
├── README.md
├── file1.js
├── file2.js
├── file3.js
├── file4.js
└── package.json
它也有自己的node_模块,但我没有提到。现在,下面是file1.js的外观:
function getResult (a, b) {
return a + b;
}
module.exports = { getResult }
以下是projectA的外观:
var calculate = require('projectB').file1.getResult; // I've tried this in several other ways too
调用中的计算结果“找不到模块错误”
。我是否在设置使用私人回购作为依赖项和/或要求它出错时做了一些根本错误的事情
更新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
需要更新以设置适当的,但默认情况下这将是index.js
。您可以执行以下操作:
// projectB/index.js
exports.file1 = require("./file1");
exports.file2 = require("./file2");
exports.file3 = require("./file3");
exports.file4 = require("./file4");
实际上,一个只从库文件导出的
index.js
是一种非常常见的模式。用于projectB
的package.json
看起来像什么?它需要声明一个main
,除非它有index.js
更新的帖子,如果这只是一堆帮助函数,main.js会是什么样子?谢谢。有趣的是,我一开始是这样的,后来我以为这是不必要的一步就把它拿走了。显然不是!