【发布时间】:2017-03-30 03:20:46
【问题描述】:
我有一个项目需要私有 repo 作为依赖项。因此,projectA 将其作为 "projectB": "user/repo" 包含在 package.json 中。这安装得很好,并列在 projectA node_modules 中。问题是,该节点在我需要依赖项功能的地方抛出错误。错误是"Cannot find module projectB"。如前所述,projectB 列在 node_modules 中。这是projectB的结构:
.
├── README.md
├── file1.js
├── file2.js
├── file3.js
├── file4.js
└── package.json
它也有自己的 node_modules,但我忽略了它。现在,这是 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
在"Cannot find module error" 中调用计算结果。在设置使用私有仓库作为依赖项和/或要求它错误时,我是否做错了什么?
更新 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"
}
}
【问题讨论】:
-
package.json对应于projectB是什么样的?它需要声明一个main,除非它有index.js -
更新帖子,如果这只是一堆辅助函数,main.js 会是什么样子?
标签: node.js package.json