Thanks to visit codestin.com
Credit goes to github.com

Skip to content

ES module imports do not work in the compiled build unless a direct path is specified #1110

@programmer2514

Description

@programmer2514

What happened: I was working on a simple project that used MJS import/export to load the express module rather than require. It worked perfectly fine via npm start, but when I built it with nexe it crashed with this error:

Error: Cannot find package 'C:\Users\...\bin\node_modules\express\package.json' imported from C:\Users\...\bin\src\App.js
Did you mean to import "express/index.js"?
    at legacyMainResolve (node:internal/modules/esm/resolve:215:26)
    at packageResolve (node:internal/modules/esm/resolve:841:14)
    at moduleResolve (node:internal/modules/esm/resolve:927:18)
    at defaultResolve (node:internal/modules/esm/resolve:1157:11)
    at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:383:12)
    at ModuleLoader.resolve (node:internal/modules/esm/loader:352:25)
    at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:227:38)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:87:39)
    at link (node:internal/modules/esm/module_job:86:36)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {

The exact same issue seems to happen with any module loaded by legacyMainResolve, when imported as follows:

import <package> from "<package name>";

However, it works perfectly if imported like this:

import <package> from "<package name>/index.js";

What you expected to happen: I expected for the import to be resolved correctly to index.js, rather than package.json.

How to reproduce it (as minimally and precisely as possible):

  1. Create a new project using npm init and npm i express
  2. Add the line "type": "module", to your package.json
  3. Create the file index.js and add the following to it
import express from "express";
const app = express();
console.log("loaded");
  1. Build and run it with nexe using the following commands:
nexe -i index.js --build -o bin/out.exe
cd bin
./out.exe
  1. The package fails to load
  2. Replace line 1 of index.js with import express from "express/index.js";
  3. Repeat step 4
  4. The package loads perfectly

Environment:

  • Platform(OS/Version): Windows 11 23H2 (OS Build 22631.4317)
  • Host Node Version: v20.14.0
  • Target Node Version: v20.14.0
  • Nexe version: 5.0.0-beta.3 (I couldn't get v4 working at all)
  • Python Version: 3.12.6

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions