Cả hai lỗi đều là lệch hệ module: file đang được Node hiểu theo một hệ (CommonJS hoặc ESM) nhưng code viết theo hệ kia.
Cannot use import statement outside a module: file bị coi là CommonJS nhưng dùngimport.ERR_REQUIRE_ESM: code CommonJSrequire()một package chỉ phát hành ESM.
Node quyết định hệ module theo: đuôi file (.mjs = ESM, .cjs = CommonJS) → nếu là .js thì xét "type" trong package.json gần nhất ("module" = ESM, mặc định/thiếu = CommonJS).
Cách xử lý, theo thứ tự nên thử:
1. Chuyển project sang ESM: thêm "type": "module" vào package.json. Lưu ý phải bỏ require, module.exports, __dirname (thay bằng import.meta.dirname).
2. Giữ CommonJS nhưng nạp package ESM bằng dynamic import: const mod = await import('pkg').
3. Đổi đuôi file lẻ sang .mjs / .cjs khi project trộn hai hệ.
Từ Node 22.12 (LTS), require() một ES module đã hoạt động mặc định (không còn cần cờ), trừ khi module đó dùng top-level await. Kiểm tra bằng process.features.require_module.