Skip to main content
0 reactions

Fixed: “[MODULE_TYPELESS_PACKAGE_JSON] Warning: Module type of file … is not specified and it doesn’t parse as CommonJS”

A Terminal window where npx auth@latest migrate first prints the Node.js warning MODULE_TYPELESS_PACKAGE_JSON, Module type of file lib/auth.ts is not specified and it doesn’t parse as CommonJS, and then lists the tables and finishes the migration.
Node.js warns about lib/auth.ts before the migration runs.

Every time I ran Better Auth’s command-line tool in a new Next.js project, Node.js printed a warning before anything else: [MODULE_TYPELESS_PACKAGE_JSON] Warning: Module type of file … is not specified and it doesn't parse as CommonJS. The command still worked, but the warning came back on every run. Here’s what causes it and the one-line fix.

The short answer: add "type": "module" to your package.json, under "private": true,. Node.js then knows that your project’s files use import and export, and the warning goes away.

What I Was Doing

I was setting up Better Auth in a new Next.js 16 app for Part 2 of the Better Auth Tutorial. I had created lib/auth.ts, the file that holds Better Auth’s settings, and ran Better Auth’s command-line tool to create the database tables:

TerminalBash
npx auth@latest migrate

The project came from create-next-app, and its package.json had no "type" field.

What I Expected

The list of tables and fields Better Auth would create, a question to confirm, and a success message.

What I Got

Node.js printed four lines of warning before Better Auth’s own output (the folder paths are shortened here):

OutputPlain text
npm notice run bookshop@0.1.0 npx
npm notice run 'auth' migrate
(node:11924) [MODULE_TYPELESS_PACKAGE_JSON] Warning: Module type of file:///…/lib/auth.ts?_1 is not specified and it doesn't parse as CommonJS.
Reparsing as ES module because module syntax was detected. This incurs a performance overhead.
To eliminate this warning, add "type": "module" to …/package.json.
(Use `node --trace-warnings ...` to show where the warning was created)
🔑 The migration will affect the following:
-> name, email, emailVerified, image, createdAt, updatedAt fields on user table.
-> expiresAt, token, createdAt, updatedAt, ipAddress, userAgent, userId fields on session table.
-> accountId, providerId, userId, accessToken, refreshToken, idToken, accessTokenExpiresAt, refreshTokenExpiresAt, scope, password, createdAt, updatedAt fields on account table.
-> identifier, value, expiresAt, createdAt, updatedAt fields on verification table.
✔ Are you sure you want to run these migrations? … yes
🚀 migration was completed successfully!
A Terminal window where npx auth@latest migrate first prints the Node.js warning MODULE_TYPELESS_PACKAGE_JSON, Module type of file lib/auth.ts is not specified and it doesn’t parse as CommonJS, and then lists the tables and finishes the migration.
Node.js warns about lib/auth.ts before the migration runs.

The migration itself worked, and the tables were created. The warning came back each time I ran the command.

What I Checked

The warning comes from Node.js itself, not from Better Auth: it starts with (node:11924), the process number of Node.js. Node.js’s own documentation explains both halves of the message:

  • Node.js runs TypeScript files by default, and it decides whether a .ts file is CommonJS or an ES module the same way as for a .js file. Its TypeScript documentation says that to use import and export, you add "type": "module" to the nearest package.json (Determining module system (opens in a new tab)).
  • A file whose nearest package.json has no "type" field is “ambiguous”: Node.js first runs it as CommonJS, and when it finds import or export, it reads the file again as an ES module. The packages documentation says this costs performance and asks you to set the field (the “type” field (opens in a new tab)).
  • lib/auth.ts starts with import lines, and the project’s package.json has no "type" field.

Why It Happens

Better Auth’s command-line tool reads your settings by loading lib/auth.ts with Node.js. With no "type" field, Node.js treats the file as CommonJS first. The first import line doesn’t parse as CommonJS, so Node.js reads the whole file again as an ES module and prints this warning to tell you that it guessed.

The Fix

Open package.json in the project folder, and add the "type" line under "private": true, at the top level of the file:

package.json (excerpt)JSON
  "private": true,
  "type": "module",

This tells Node.js that every .js and .ts file in the project is an ES module, so it reads lib/auth.ts once, as an ES module, with no guess. Next.js kept working the same in my project: npm run dev and npm run build both ran normally with the new line.

After the Fix

I ran the same command again:

TerminalBash
npx auth@latest migrate
OutputPlain text
npm notice run bookshop@0.1.0 npx
npm notice run 'auth' migrate
🚀 No migrations needed.
A Terminal window where npx auth@latest migrate prints only the two npm notice lines and No migrations needed, without any Node.js warning.
The same command after adding the type field: no warning.

The warning is gone. It says “No migrations needed” because the first run had already created the tables.

Common Mistakes

MistakeWhat happensDo this instead
Putting "type": "module" inside "scripts" or another sectionNode.js only reads the field at the top level, so the warning staysAdd it at the top level, next to "private"
Renaming lib/auth.ts to get rid of the warningBetter Auth’s command-line tool looks for a file named auth.tsKeep the name and add the "type" field

Troubleshooting

ProblemCauseFix
The warning names another fileAnother file with import or export is loaded the same wayThe same "type" field covers every file under that package.json
The warning is still thereThe line went into a different package.jsonEdit the package.json in the folder where you run the command

FAQ

Is the warning harmful?

No. Node.js reads the file again and the command works. The cost is the extra reading, which Node.js’s documentation calls a performance cost.

Does “type”: “module” break Next.js?

It didn’t in my Next.js 16 project: the development server and the production build both worked the same with it.

Conclusion

The MODULE_TYPELESS_PACKAGE_JSON warning means Node.js had to guess whether a file is CommonJS or an ES module. One line in package.json, "type": "module", answers the question, and Better Auth’s command-line tool runs without it.

Additional Resources

Better Auth, Next.js and other names mentioned are trademarks of their respective owners. JD Bots isn’t affiliated with or endorsed by Better Auth, Inc. or Vercel.

0 comments