【发布时间】:2013-04-03 17:46:07
【问题描述】:
我正在研究一个使用 C++ 开发 Nodejs 扩展的 Hello World 示例。一切正常,我可以运行该示例。但是我想使用 require('hello') 而不是 require('./build/Release/hello') 我知道需要将文件放在node_modules 文件夹。当我按照有关使用 NPM Install 本地安装包的说明进行操作时,不会创建文件夹 node_modules (经过几个小时我已经开发了一种解决方法,但它是一团糟)。
我使用的是 Mac OS Mountain Lion 和 NPM 版本 1.2.17。 NPM 从本地和全局的存储库(和卸载)安装包没有任何问题。我检查了 NPM 根,它指向一个 node_modules 文件夹并按照previous question 中的建议重新安装了 NPM。文件如下:
package.json
{
"name": "HelloWorld",
"version": "1.0.0",
"description": "Nodejs Extension using C++",
"main": "./build/Release/hello.node",
"scripts": {
"preinstall": "node-gyp rebuild",
"preuninstall": "rm -rf build/*"
},
"repository": "",
"readmeFilename": "README.md",
"author": "",
"license": ""
}
binding.gyp
{
"targets": [
{
"target_name": "hello",
"sources" : [ "src/hello.cc" ]
}
]
}
hello.cc
#include <node.h>
#include <v8.h>
using namespace v8;
Handle<Value> Method(const Arguments& args) {
HandleScope scope;
return scope.Close(String::New("Hello, World!"));
}
void init(Handle<Object> exports) {
exports ->Set(String::NewSymbol("hello"),
FunctionTemplate::New(Method)->GetFunction());
}
NODE_MODULE(hello, init)
由于缺乏使用 NPM 的经验,我觉得我遗漏了一些简单的东西,因此希望得到任何帮助。
另外,我是 Stack Overflow 的新手,因此我们将不胜感激地收到任何关于如何改进未来问题的指导。
【问题讨论】: