node.js 앱간에 코드를 공유하는 방법은 무엇입니까?
내가 작성한 몇 가지 모듈을 모두 공유하는 노드에 여러 앱이 있습니다. 이러한 모듈은 npm을 통해 사용할 수 없습니다. 앱간에 자유롭게 공유하고 싶지만 디렉터리를 복사하거나 Git에 의존하고 싶지는 않습니다. 그리고 나는 이것을하기 위해 심볼릭 링크를 사용하는 것을별로 좋아하지 않는다.
다음과 같이 디렉토리를 정렬하고 싶습니다.
app1
server.js
node_modules
(public modules from npm needed for app1)
lib
(my own modules specific to app1)
app2
server.js
node_modules
(public modules from npm needed for app2)
lib
(my own modules specific to app2)
shared_lib
(my own modules that are used in both app1 and app2)
내가보고있는 문제는 shared_lib의 모듈이 실행중인 앱의 node_modules 디렉토리에서 모듈을 찾을 위치에 대해 혼란스러워하는 것 같습니다. 적어도 그게 문제라고 생각합니다.
그래서 .... 파일의 중복을 피하는 좋은 방법은 무엇입니까? (내 코드가 아니기 때문에 node_modules의 중복 항목에 대해서는 신경 쓰지 않으며 Git 등으로 체크인하지 않습니다)
다른 수준의 node_modules 폴더 를 사용 하여이 작업을 수행했습니다. 노드는 모듈을 찾을 때까지 자동으로 위로 이동합니다.
node_modules 내부에 모듈을 포함하기 위해 npm에 게시 할 필요는 없습니다. 다음을 사용하십시오.
"private": true
각 개인 package.json 파일 내부에 프로젝트의 경우 다음이 있습니다.
app1
server.js
node_modules
(public modules from npm needed for app1)
(private modules locally needed for app1)
app2
server.js
node_modules
(public modules from npm needed for app2)
(private modules locally needed for app2)
node_modules
(public modules from npm needed for app1 & app2)
(private modules locally for app1 & app2)
요점은 node.js는 이미 이것을 처리하는 메커니즘을 가지고 있으며 훌륭합니다. 'NPM이 아닌 개인'트릭과 결합하면됩니다.
간단히 말해서 :
require('somemodule')
앱 A 또는 B에서 모듈을 찾을 때까지 위로 캐스케이드됩니다. 사실-이것은 require (...) 문 을 변경하지 않고 위치를 핫스왑 할 수있게합니다 .
npm 문서에서는 npm-link 를 사용하여 로컬에서 고유 한 Node.js 패키지를 만든 다음 다른 Node.js 응용 프로그램에서 사용할 수 있도록 할 것을 권장 합니다. 간단한 4 단계 과정입니다.
일반적인 절차는 먼저 다음 구조로 패키지를 만드는 것입니다.
hello
| index.js
| package.json
이러한 파일의 일반적인 구현은 다음과 같습니다.
index.js
exports.world = function() {
return('Hello World');
}
package.json
{
"name": "hello",
"version": "0.0.1",
"private": true,
"main": "index.js",
"dependencies": {
},
"engines": {
"node": "v0.6.x"
}
}
"private : true"는 npm이 패키지 게시를 거부하도록합니다. 이것은 개인 패키지가 실수로 게시되는 것을 방지하는 방법입니다.
그런 다음 Node.js 패키지 폴더의 루트로 이동하고 실행 npm link
하여 패키지를 전역으로 연결하여 다른 애플리케이션에서 사용할 수 있도록합니다.
이 패키지를 다른 응용 프로그램 (예 : "hello-world")에서 사용하려면 다음 디렉터리 구조를 사용합니다.
hello-world
| app.js
hello-world 폴더로 이동하여 다음을 실행합니다.
npm link hello
Now you can use it like any other npm package like so:
app.js
var http = require('http');
var hello = require('hello');
var server = http.createServer(function(req, res) {
res.writeHead(200);
res.end(hello.world());
});
server.listen(8080);
Just use the correct path in your require call
For example in server.js that would be:
var moduleName = require('../shared_lib/moduleName/module.js');
Its Important to know that as soon as your path is prefixed with '/', '../', or './' the path is relative to the calling file.
For further information about nodes module loading visit: http://nodejs.org/docs/latest/api/modules.html
Yes, you can reference shared_lib from app1, but then you run into a problem if you want to package and deploy app1 to some other environment, such as a web server on AWS.
In this case, you're better off installing your modules in shared_lib to app1 and app2 using "npm install shared_lib/module". It will also install all the dependencies of the shared_lib modules in app1 and app2 and deal with conflicts/duplicates.
See this: How to install a private NPM module without my own registry?
If you check out the node.js docs, you'll see that Node.js understands the package.json
file format as well, at least cursorily.
Basically, if you have a directory named foo
, and in that directory is a package.json
file with the key-value pair: "main": "myCode.js"
, then if you try to require("foo")
and it finds this directory with a package.json
file inside, it will then use foo/myCode.js
for the foo
module.
So, with your directory structure, if each shared lib has it's own directory with such a simple package.json
file inside, then your apps can get the shared libs by:
var lib1 = require('../shared_lib/lib1');
var lib2 = require('../shared_lib/lib2');
And that should work for both of these apps.
참고URL : https://stackoverflow.com/questions/11278921/how-to-share-code-between-node-js-apps
'Development Tip' 카테고리의 다른 글
iOS 6.0이 설치된 Xcode 4.5에서 요청하지 않은 로그 메시지 (0) | 2020.11.18 |
---|---|
std :: fill (0)이 std :: fill (1)보다 느린 이유는 무엇입니까? (0) | 2020.11.18 |
Qt 정적 연결 및 배포 (0) | 2020.11.18 |
Covered Index 란 무엇입니까? (0) | 2020.11.18 |
Rails has_one : 연결을 통해 (0) | 2020.11.18 |