[GH-ISSUE #2] Error on execution #2

Closed
opened 2026-02-27 19:20:51 +03:00 by kerem · 6 comments
Owner

Originally created by @sebastienbarre on GitHub (Nov 10, 2025).
Original GitHub issue: https://github.com/f/git-rewrite-commits/issues/2

Thank you for making this tool available.
On macOS 26.1 (MacBook Pro M4), I'm getting errors after running:

$ npx git-rewrite-commits --max-commits 10 --api-key "..."

I abbreviated the path names below.
Note that I have no git/filter-msg.js, I assume you are trying to create that file?


🔄 Rewriting git history...
file://[ABBREVIATED]/monorepo/.git/filter-msg.js:2
const fs = require('fs');
           ^

ReferenceError: require is not defined in ES module scope, you can use import instead
This file is being treated as an ES module because it has a '.js' file extension and '[ABBREVIATED]/monorepo/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
    at file://[ABBREVIATED]/monorepo/.git/filter-msg.js:2:12
    at ModuleJob.run (node:internal/modules/esm/module_job:377:25)
    at async onImport.tracePromise.__proto__ (node:internal/modules/esm/loader:691:26)
    at async asyncRunEntryPointWithESMLoader (node:internal/modules/run_main:101:5)

Node.js v24.11.0
msg filter failed: node [ABBREVIATED]/monorepo/.git/filter-msg.js

❌ Error rewriting history: Command failed: git filter-branch -f --msg-filter 'node [ABBREVIATED]/monorepo/.git/filter-msg.js' HEAD
Command failed: git filter-branch -f --msg-filter 'node [ABBREVIATED]/monorepo/.git/filter-msg.js' HEAD
file://[ABBREVIATED]/monorepo/.git/filter-msg.js:2
const fs = require('fs');
           ^

ReferenceError: require is not defined in ES module scope, you can use import instead
This file is being treated as an ES module because it has a '.js' file extension and '[ABBREVIATED]/monorepo/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
    at file://[ABBREVIATED]/monorepo/.git/filter-msg.js:2:12
    at ModuleJob.run (node:internal/modules/esm/module_job:377:25)
Originally created by @sebastienbarre on GitHub (Nov 10, 2025). Original GitHub issue: https://github.com/f/git-rewrite-commits/issues/2 Thank you for making this tool available. On macOS 26.1 (MacBook Pro M4), I'm getting errors after running: ``` $ npx git-rewrite-commits --max-commits 10 --api-key "..." ``` I abbreviated the path names below. Note that I have no `git/filter-msg.js`, I assume you are trying to create that file? ``` 🔄 Rewriting git history... file://[ABBREVIATED]/monorepo/.git/filter-msg.js:2 const fs = require('fs'); ^ ReferenceError: require is not defined in ES module scope, you can use import instead This file is being treated as an ES module because it has a '.js' file extension and '[ABBREVIATED]/monorepo/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension. at file://[ABBREVIATED]/monorepo/.git/filter-msg.js:2:12 at ModuleJob.run (node:internal/modules/esm/module_job:377:25) at async onImport.tracePromise.__proto__ (node:internal/modules/esm/loader:691:26) at async asyncRunEntryPointWithESMLoader (node:internal/modules/run_main:101:5) Node.js v24.11.0 msg filter failed: node [ABBREVIATED]/monorepo/.git/filter-msg.js ❌ Error rewriting history: Command failed: git filter-branch -f --msg-filter 'node [ABBREVIATED]/monorepo/.git/filter-msg.js' HEAD Command failed: git filter-branch -f --msg-filter 'node [ABBREVIATED]/monorepo/.git/filter-msg.js' HEAD file://[ABBREVIATED]/monorepo/.git/filter-msg.js:2 const fs = require('fs'); ^ ReferenceError: require is not defined in ES module scope, you can use import instead This file is being treated as an ES module because it has a '.js' file extension and '[ABBREVIATED]/monorepo/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension. at file://[ABBREVIATED]/monorepo/.git/filter-msg.js:2:12 at ModuleJob.run (node:internal/modules/esm/module_job:377:25) ```
kerem closed this issue 2026-02-27 19:20:51 +03:00
Author
Owner

@f commented on GitHub (Nov 10, 2025):

It should be creating that file.

<!-- gh-comment-id:3509842730 --> @f commented on GitHub (Nov 10, 2025): It should be creating that file.
Author
Owner

@sebastienbarre commented on GitHub (Nov 10, 2025):

Yes I assume it is, but seems to be failing at it.
The repo is private, but I can share more info if needed.
I'm running node v24.11.0

<!-- gh-comment-id:3514370718 --> @sebastienbarre commented on GitHub (Nov 10, 2025): Yes I assume it is, but seems to be failing at it. The repo is private, but I can share more info if needed. I'm running node v24.11.0
Author
Owner

@sebastienbarre commented on GitHub (Nov 14, 2025):

require is not defined in ES module scope, you can use import instead
This file is being treated as an ES module because it has a '.js' file extension and 'package.json' contains "type": "module"

Anybody who runs this script in a repo where package.json has "type": "module" will encounter the same problem apparently. Unfortunately I can't change it temporarily to "commonjs" because it then errors with "Cannot rewrite branches: You have unstaged changes.", and if I try to commit it everything in my lint-staged blows up (and rejects the commit).

<!-- gh-comment-id:3530466412 --> @sebastienbarre commented on GitHub (Nov 14, 2025): ``` require is not defined in ES module scope, you can use import instead This file is being treated as an ES module because it has a '.js' file extension and 'package.json' contains "type": "module" ``` Anybody who runs this script in a repo where `package.json` has `"type": "module"` will encounter the same problem apparently. Unfortunately I can't change it temporarily to "commonjs" because it then errors with "Cannot rewrite branches: You have unstaged changes.", and if I try to commit it everything in my lint-staged blows up (and rejects the commit).
Author
Owner

@willtheorangeguy commented on GitHub (Nov 14, 2025):

Should be fixed in #15. @sebastienbarre do you think you can test the changes on Mac?

<!-- gh-comment-id:3534981811 --> @willtheorangeguy commented on GitHub (Nov 14, 2025): Should be fixed in #15. @sebastienbarre do you think you can test the changes on Mac?
Author
Owner

@f commented on GitHub (Nov 15, 2025):

Should be fixed with the new version.

<!-- gh-comment-id:3535396626 --> @f commented on GitHub (Nov 15, 2025): Should be fixed with the new version.
Author
Owner

@sebastienbarre commented on GitHub (Nov 15, 2025):

It is executing now, but there is something FUNDAMENTALLY BROKEN right now with this script, which will very much mess with people's repository. Hopefully this is only with respect to the "--max-commits" option.

I can't afford to reproduce it more, but I ran it this way, limiting myself to 40 commits to be safe:

npx git-rewrite-commits --max-commits 40

I have about 230+ commits in my repo; let’s say 240 for the sake of this explanation. I’ll call commit #1 the very first (oldest) commit, and commit #240 the most recent. The script inspected my repo starting with the most recent commit (#240) and counting down 40 of them (back to #201).

The last (most recent) 35 commits were already using conventional commit messages (i.e., commits #240 back through #206). The script correctly identified them as such and ignored them. It then analyzed the 5 commits before that and suggested good replacements (i.e. commits #205 to #201, going back in time).

So at that point, I could see 40 commit messages on screen with nice, conventional messages—5 of them coming from this script. I confirmed the choice.

This is where it went very wrong!

Instead of modifying commits #240 to #201, it left them untouched and used those new messages to overwrite commits #40 down to #1 instead! In other words, instead of rewriting the messages at the top of the history, where they were found, it rewrote them at the bottom.

As a result, I’ve now “lost” the original messages for the first (oldest) 40 commits in my repo.

For example, this commit is #203, from Nov 6, 2025 (the 38th this script inspected):

Image

The script suggested "refactor(AGENTS.md): Refactor content structure for clarity and consistency".

Fair enough as a message, except that it was left untouched and it’s now the message for commit #3 instead, which originally dated from Oct 3, 2025 and has nothing to do with this file. Similarly, the message intended for commit #202 is now slapped on commit #2, and the message for commit #201 is now on commit #1 instead; my oldest commit. I’ve lost the original “First commit” message.

Image

Thankfully this is a recent, private repo, but it is borked now.
The order and contents of the commits seem fine, but my most recent 40 commit messages have now overwritten my first (oldest) 40 commit messages.

<!-- gh-comment-id:3536134039 --> @sebastienbarre commented on GitHub (Nov 15, 2025): It is executing now, but there is something FUNDAMENTALLY BROKEN right now with this script, which will very much mess with people's repository. Hopefully this is only with respect to the "--max-commits" option. I can't afford to reproduce it more, but I ran it this way, limiting myself to 40 commits to be safe: ``` npx git-rewrite-commits --max-commits 40 ``` I have about 230+ commits in my repo; let’s say 240 for the sake of this explanation. I’ll call commit #1 the very first (oldest) commit, and commit #240 the most recent. The script inspected my repo starting with the most recent commit (#240) and counting down 40 of them (back to #201). The last (most recent) 35 commits were already using conventional commit messages (i.e., commits #240 back through #206). The script correctly identified them as such and ignored them. It then analyzed the 5 commits before that and suggested good replacements (i.e. commits #205 to #201, going back in time). So at that point, I could see 40 commit messages on screen with nice, conventional messages—5 of them coming from this script. I confirmed the choice. This is where it went very wrong! Instead of modifying commits #240 to #201, it left them untouched and used those new messages to overwrite commits #40 down to #1 instead! In other words, instead of rewriting the messages at the top of the history, where they were found, it rewrote them at the bottom. As a result, I’ve now “lost” the original messages for the first (oldest) 40 commits in my repo. For example, this commit is #203, from Nov 6, 2025 (the 38th this script inspected): <img width="1295" height="112" alt="Image" src="https://github.com/user-attachments/assets/a978ddc8-d38f-4ef8-9108-4abf9e0c6ce2" /> The script suggested "refactor(AGENTS.md): Refactor content structure for clarity and consistency". Fair enough as a message, except that it was left untouched and it’s now the message for commit #3 instead, which originally dated from Oct 3, 2025 and has nothing to do with this file. Similarly, the message intended for commit #202 is now slapped on commit #2, and the message for commit #201 is now on commit #1 instead; my oldest commit. I’ve lost the original “First commit” message. <img width="1284" height="255" alt="Image" src="https://github.com/user-attachments/assets/160defc5-9b1e-4564-bbab-a08145da2ba7" /> Thankfully this is a recent, private repo, but it is borked now. The order and contents of the commits seem fine, but my most recent 40 commit *messages* have now overwritten my first (oldest) 40 commit messages.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/git-rewrite-commits#2
No description provided.