私有仓库依赖项安装,但“找不到模块”

时间:2017-03-29 16:59:03

标签: node.js package.json

我有一个项目需要私有仓库作为依赖项。因此,projectA将它包含在package.json中作为"projectB": "user/repo"。这安装得很好,并在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"
  }
}

1 个答案:

答案 0 :(得分:3)

需要更新

projectB以设置适当的the ListViewItem(string) constructor sets the Text property by default,但默认情况下会为index.js。您可以执行以下操作:

// projectB/index.js
exports.file1 = require("./file1");
exports.file2 = require("./file2");
exports.file3 = require("./file3");
exports.file4 = require("./file4");

实际上这是一种非常常见的模式,index.js只能从库文件中导出。