Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Error related to global.node_api_dotnet.require when trying to use streams

未关闭
#420 1 条评论 0 个 reaction 已指派 1 人 在 GitHub 查看

@jasongin 已经在做这个了。

开始于 2025年3月14日。

评估

这个 Issue 还没有评估数据。

描述

I tried a simple example to test out using streams:

[JSExport]
public class Class1
{
    public static Stream HelloStream(string greeter)
    {
        string message = "hello " + greeter;
        byte[] messageBytes = Encoding.UTF8.GetBytes(message);
        MemoryStream stream = new MemoryStream(messageBytes);
        stream.Position = 0;
        return stream;
    }
}

Oddly, this throws an error:

const MyModule = require('./MyModule/bin/MyModule');
const stream = MyModule.Class1.helloStream('world'); // throws

This is the error:

Error: The require function was not found on the global node_api_dotnet object. Set `global.node_api_dotnet.require` before loading the module.
    at Microsoft.JavaScript.NodeApi.Interop.JSRuntimeContext.get_RequireFunction() + 0x35f
    at Microsoft.JavaScript.NodeApi.Interop.JSRuntimeContext.<>c__DisplayClass51_0.<Import>b__0(ValueTuple`2 _) + 0x1c4
    at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey, Func`2) + 0xdf
    at Microsoft.JavaScript.NodeApi.Interop.JSRuntimeContext.Import(String, String, Boolean) + 0xd7
    at Microsoft.JavaScript.NodeApi.Interop.JSRuntimeContext.<>c__DisplayClass51_0.<Import>b__0(ValueTuple`2 _) + 0x33e
    at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey, Func`2) + 0xdf
    at Microsoft.JavaScript.NodeApi.Interop.JSRuntimeContext.Import(String, String, Boolean) + 0xd7
    at Microsoft.JavaScript.NodeApi.Interop.NodeStream.CreateProxy(Stream) + 0x5a
    at Microsoft.JavaScript.NodeApi.Interop.JSRuntimeContext.<>c__DisplayClass37_0`1.<GetOrCreateObjectWrapper>b__0() + 0x43
    at Microsoft.JavaScript.NodeApi.Interop.JSRuntimeContext.GetOrCreateObjectWrapper[T](T, Func`1) + 0xd6
    at Microsoft.JavaScript.NodeApi.Interop.JSRuntimeContext.GetOrCreateObjectWrapper[T](T) + 0x8d
    at Microsoft.JavaScript.NodeApi.Generated.Module.MyModule_Class1_HelloStream(JSCallbackArgs __args) + 0xa5
    at MyModule!<BaseAddress>+0x145159
    at Microsoft.JavaScript.NodeApi.JSValue.InvokeCallback[TDescriptor](JSRuntime.napi_env, JSRuntime.napi_callback_info, JSValueScopeType, Func`2) + 0x1bc
    at Object.<anonymous> (C:\Users\User\project\index.js:5:37)
    at Module._compile (node:internal/modules/cjs/loader:1105:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
    at node:internal/main/run_main_module:17:47
error Command failed with exit code 1.

I was able to fix it by adding global.node_api_dotnet = { require } before requiring the module, but this feels like a hack - am I doing something wrong?

主要语言
C#
星标
785
派生
80
PR 合并指标
30 天内没有已合并 PR

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

microsoft/node-api-dotnet 的其他 Issue

查看 microsoft/node-api-dotnet 的全部 Issue

相似的 Issue

更多 C# Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。