私人回购依赖项安装,但“无法find模块”
我有一个项目,需要一个私人回购作为依赖。 所以,projectA把package.json包含在"projectB": "user/repo"
。 这安装得很好,并在projectA node_modules中列出。 问题是,该节点抛出错误,我需要依赖的function。 错误是"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" } }
projectB
需要更新来设置一个合适的main
,但默认情况下这将是index.js
。 你可以做如下的事情:
// projectB/index.js exports.file1 = require("./file1"); exports.file2 = require("./file2"); exports.file3 = require("./file3"); exports.file4 = require("./file4");
这实际上是一个非常常见的模式,有一个index.js
,除了从库文件导出。