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

Generated entry JS file for AOT thinks I'm not using AOT

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

@jasongin 已经在做这个了。

开始于 2025年3月14日。

评估

这个 Issue 还没有评估数据。

描述

I'm building an AOT module with the following .csproj file:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <PublishAot>true</PublishAot>
    <PublishNodeModule>true</PublishNodeModule>
    <PublishDir>bin</PublishDir>
    <PackNpmPackage>true</PackNpmPackage>
    <PackageOutputPath>pkg</PackageOutputPath>
    <RuntimeIdentifier>win-x64</RuntimeIdentifier>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.JavaScript.NodeApi" Version="0.9.5" />
    <PackageReference Include="Microsoft.JavaScript.NodeApi.Generator" Version="0.9.5" />
  </ItemGroup>

</Project>

After dotnet publish, I get a valid AOT .dll file with the following .js entry file generated:

// Generated for: NativeModule 1.0.0.0
// Generated by: Microsoft.JavaScript.NodeApi.Generator 0.9.0.0
/* eslint-disable */

const dotnet = require('node-api-dotnet/net8.0');
const path = require('node:path');
// @ts-ignore - https://github.com/DefinitelyTyped/DefinitelyTyped/discussions/65252
const { dlopen, platform, arch } = require('node:process');

const moduleName = path.basename(__filename, __filename.match(/(\.[cm]?js)?$/)[0]);
module.exports = dotnet ? importDotnetModule(moduleName) : importAotModule(moduleName);

function importDotnetModule(moduleName) {
	[...]
}

function importAotModule(moduleName) {
	[...]
}

Note the module.exports = dotnet ? importDotnetModule(moduleName) : importAotModule(moduleName); line which is the culprit here - because I'm using AOT, it's supposed to call importAotModule rather than importDotnetModule. However, I'm not sure how this is supposed to work:

  • If I have node-api-dotnet in my dependencies, require('node-api-dotnet/net8.0') works, i.e. it returns a truthy value, so importDotnetModule gets called (which fails)
  • If I don't have node-api-dotnet in my dependencies (as in the example), the require throws and I can't import the module

I feel I'm missing something here, how am I supposed to "tell" the generator or runtime I'm using AOT?

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

环境准备

从这里开始

  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 摘要。