npm不会为自己的本地安装包创build./node_modules

我正在通过一个Hello World示例来开发使用C ++的Nodejs扩展。 一切正常,我可以运行这个例子。 不过,我想使用require('hello')而不是require('./ build / Release / hello') ,我知道需要将文件放在node_modules文件夹中。 当我按照有关使用NPM安装到本地安装程序包的说明进行操作时,并未创build文件夹node_modules (几个小时后,我开发了一个解决方法,但是很混乱)。

我正在使用Mac OS Mountain Lion和NPM版本1.2.17。 NPM从本地和全局安装存储库中的软件包(并卸载),没有任何问题。 我已经检查了NPM根目录 ,它指向一个node_modules文件夹,并按照上一个问题的build议重新安装了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) 

我觉得我缺less一些简单的东西,因为我缺乏使用NPM的经验,因此会很感激任何帮助。

另外,我是Stack Overflow的新手,因此我将非常乐意接受有关如何改进任何未来问题的指导。

包的名称由package.json中的名称属性确定。 你有它的方式设置将与

 `require("HelloWorld")` 

像你一样

 `"name": "HelloWorld"` 

如果你想要它

 `require("hello")` 

只需改变你的package.json文件就可以了

 `"name": "hello"` 

到你的安装问题 – 如何和在哪里运行npm安装? 我创build了一个与HelloWorld同级别的HelloWorldClient目录并运行

 `npm install ../HelloWorld/` 

哪些工作正常。 我的客户端代码(将包名更改为hello)也是如此:test.js:

 var hello = require('hello'); console.log(hello.hello());