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