ESM build cannot be imported in Node.js: `dist/` emits extensionless relative specifiers

Đang mở Phù hợp với người mới
#4 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
72/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
node.js, typescript
Lĩnh vực
build-system

Hướng nghiên cứu

Đầu ra ESM được tạo bởi tsconfig.build.json, file này đặt module là ESNext và moduleResolution là bundler, nên các import tương đối như ./api vẫn không có phần mở rộng trong dist/*.js. Hãy bắt đầu bằng việc đọc cấu hình này và script build trong package.json, sau đó tái hiện lỗi bằng cách chạy build và import dist/index.js bằng node. Hoàn thành khi dist ESM tải được trên Node mà không gặp ERR_MODULE_NOT_FOUND và đầu ra CJS vẫn hoạt động.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

Importing @craftycodie/cstruct in a Node.js ES module fails at load time with ERR_MODULE_NOT_FOUND, whereas require("@craftycodie/cstruct") works via dist-cjs/index.js.

npm init -y && npm install @craftycodie/[email protected]
node --input-type=module -e 'import { c } from "@craftycodie/cstruct"; console.log(c.sizeof(c.struct({ x: c.u8() })));'

Actual output:

Error [ERR_MODULE_NOT_FOUND]: Cannot find module '.../node_modules/@craftycodie/cstruct/dist/api' imported from .../node_modules/@craftycodie/cstruct/dist/index.js

Expected output:

1

tsconfig.build.json compiles the ESM output with "module": "ESNext" and "moduleResolution": "bundler", so tsc preserves extensionless relative and directory specifiers (such as "./api" and "./field-types") in dist/*.js, which Node's ESM resolver rejects.

Versions tested: @craftycodie/[email protected] and main (9421e72), Node.js v26.13.0.

Credit: https://ucsbplse.github.io/

Ngôn ngữ chính
TypeScript
Star
4
Fork
0
Merge trung bình
21 ngày 8 giờ
Pull request đã merge (30 ngày)
1

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue tương tự

Thêm issue về TypeScript

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.