c++ - Node JS - 模块没有自行注册

标签 c++ node.js node-gyp node.js-addon

我浏览了不同的帖子,其中大多数建议重建/重新安装/更新 npm,我试过了,但没有帮助。

我正在尝试使用 C++ 的 node.js 示例,下面是文件详细信息
测试 Node .cpp

#include<iostream>
class TestNode {
public:
    void printHelloW() {
        std::cout << "Hello World!!!";
    }
};


绑定(bind).gyp

{
  "targets": [
    {
      "target_name": "newexample",
      "sources": [ "TestNode.cpp" ]
    }
  ]
}

现在执行并抛出错误,指出模块未自行注册。

D:\newexample>node-gyp configure
gyp info it worked if it ends with ok
gyp info using node-gyp@3.3.1
gyp info using node@5.10.0 | win32 | x64
gyp info spawn C:\Python27\python.exe
gyp info spawn args [ 'C:\\Users\\rtv\\AppData\\Roaming\\npm\\node_modules\\node-gyp\\gyp\\gyp_main.py',
gyp info spawn args   'binding.gyp',
gyp info spawn args   '-f',
gyp info spawn args   'msvs',
gyp info spawn args   '-G',
gyp info spawn args   'msvs_version=auto',
gyp info spawn args   '-I',
gyp info spawn args   'D:\\newexample\\build\\config.gypi',
gyp info spawn args   '-I',
gyp info spawn args   'C:\\Users\\rtv\\AppData\\Roaming\\npm\\node_modules\\node-gyp\\addon.gypi',
gyp info spawn args   '-I',
gyp info spawn args   'C:\\Users\\rtv\\.node-gyp\\5.10.0\\include\\node\\common.gypi',
gyp info spawn args   '-Dlibrary=shared_library',
gyp info spawn args   '-Dvisibility=default',
gyp info spawn args   '-Dnode_root_dir=C:\\Users\\rtv\\.node-gyp\\5.10.0',
gyp info spawn args   '-Dnode_gyp_dir=C:\\Users\\rtv\\AppData\\Roaming\\npm\\node_modules\\node-gyp',
gyp info spawn args   '-Dnode_lib_file=node.lib',
gyp info spawn args   '-Dmodule_root_dir=D:\\newexample',
gyp info spawn args   '--depth=.',
gyp info spawn args   '--no-parallel',
gyp info spawn args   '--generator-output',
gyp info spawn args   'D:\\newexample\\build',
gyp info spawn args   '-Goutput_dir=.' ]
gyp info ok

D:\newexample>node-gyp build
gyp info it worked if it ends with ok
gyp info using node-gyp@3.3.1
gyp info using node@5.10.0 | win32 | x64
gyp info spawn C:\Program Files (x86)\MSBuild\14.0\bin\msbuild.exe
gyp info spawn args [ 'build/binding.sln',
gyp info spawn args   '/clp:Verbosity=minimal',
gyp info spawn args   '/nologo',
gyp info spawn args   '/p:Configuration=Release;Platform=x64' ]
Building the projects in this solution one at a time. To enable parallel build, please add the "/m" switch.
  TestNode.cpp
  win_delay_load_hook.c
  Generating code
  Finished generating code
  newexample.vcxproj -> D:\newexample\build\Release\\newexample.node
gyp info ok

D:\newexample>node
> var obj=require("./build/Release/newexample")
Error: Module did not self-register.
    at Error (native)
    at Object.Module._extensions..node (module.js:440:18)
    at Module.load (module.js:357:32)
    at Function.Module._load (module.js:314:12)
    at Module.require (module.js:367:17)
    at require (internal/module.js:16:19)
    at repl:1:9
    at REPLServer.defaultEval (repl.js:269:27)
    at bound (domain.js:287:14)
    at REPLServer.runBound [as eval] (domain.js:300:12)
>

版本信息

D:\newexample>node -v
v5.10.0

D:\newexample>npm -v
3.8.3

D:\newexample>node-gyp -v
v3.3.1

OS: Windows 7 Professional 64-bit

请您指导我解决此错误。

最佳答案

编写 node.js 插件并不是那么简单,您需要显式导出函数并调用适当的宏以将插件注册到 Node 的模块系统。阅读 Addons documentation获取更多信息和示例。

关于c++ - Node JS - 模块没有自行注册,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36463472/

相关文章:

c++ - 如何迭代 IO Kit 的键?

c++ - 从字面上理解 RegEx 中的每个字符

c++ - 来自 ISAPI 扩展的清空 _ASSERTE 对话框

javascript - 如何将 Canvas 数据 uri 上传到 amazon s3 服务器

node.js - 在 heroku 中使用 nodejs 的 C++ 模块

c++ - 如何使用boost从内存映射文件访问内存块?

node.js - Mongoose - foreach 循环与聚合结果

javascript - 在 Mongoose 文档获取时自动填充 ()

node.js - 通过命令行传递时,node-gyp 变量的行为有所不同

node.js - Electron 重建中发生未处理的错误