最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

drizzle-kit generate `SyntaxError: Unexpected token in JSON at position 0` on mac - Stack Overflow

programmeradmin1浏览0评论

I am using drizzle ORM to manage database migrations.

After making changes to my Schema.ts file, I ran drizzle-kit generate --config=drizzle.config.ts --name MigrationName

This produced an error:

john@Johns-MacBook-Pro repo % yarn db:generate Test
yarn run v1.22.22
$ drizzle-kit generate --config=drizzle.config.ts --name Test
Reading config file '/Users/john/Documents/code/repo/drizzle.config.ts'
SyntaxError: Unexpected token  in JSON at position 0
    at JSON.parse (<anonymous>)
    at snapshots.reduce.malformed (/Users/john/Documents/code/repo/node_modules/drizzle-kit/bin.cjs:6955:29)
    at Array.reduce (<anonymous>)
    at validateWithReport (/Users/john/Documents/code/repo/node_modules/drizzle-kit/bin.cjs:6953:32)
    at prepareMigrationFolder (/Users/john/Documents/code/repo/node_modules/drizzle-kit/bin.cjs:6995:22)
    at prepareAndMigratePg (/Users/john/Documents/code/repo/node_modules/drizzle-kit/bin.cjs:33066:40)
    at Object.handler (/Users/john/Documents/code/repo/node_modules/drizzle-kit/bin.cjs:88522:13)
    at async run (/Users/john/Documents/code/repo/node_modules/drizzle-kit/bin.cjs:87056:7)
✨  Done in 0.61s.

My drizzle config file is fairly vanilla:

drizzle.config.ts:

import { defineConfig } from 'drizzle-kit';

export default defineConfig({
    out: './src/database/migrations',
    schema: './src/database/Schema.ts',
    dialect: 'postgresql',
    dbCredentials: { url: process.env.DB_URL ?? '' },
});

and I confirmed process.env.DB_URL is correct by adding a console.log(process.env.DB_URL) to drizzle.config.ts.

Any idea what could be causing this?

I am using drizzle ORM to manage database migrations.

After making changes to my Schema.ts file, I ran drizzle-kit generate --config=drizzle.config.ts --name MigrationName

This produced an error:

john@Johns-MacBook-Pro repo % yarn db:generate Test
yarn run v1.22.22
$ drizzle-kit generate --config=drizzle.config.ts --name Test
Reading config file '/Users/john/Documents/code/repo/drizzle.config.ts'
SyntaxError: Unexpected token  in JSON at position 0
    at JSON.parse (<anonymous>)
    at snapshots.reduce.malformed (/Users/john/Documents/code/repo/node_modules/drizzle-kit/bin.cjs:6955:29)
    at Array.reduce (<anonymous>)
    at validateWithReport (/Users/john/Documents/code/repo/node_modules/drizzle-kit/bin.cjs:6953:32)
    at prepareMigrationFolder (/Users/john/Documents/code/repo/node_modules/drizzle-kit/bin.cjs:6995:22)
    at prepareAndMigratePg (/Users/john/Documents/code/repo/node_modules/drizzle-kit/bin.cjs:33066:40)
    at Object.handler (/Users/john/Documents/code/repo/node_modules/drizzle-kit/bin.cjs:88522:13)
    at async run (/Users/john/Documents/code/repo/node_modules/drizzle-kit/bin.cjs:87056:7)
✨  Done in 0.61s.

My drizzle config file is fairly vanilla:

drizzle.config.ts:

import { defineConfig } from 'drizzle-kit';

export default defineConfig({
    out: './src/database/migrations',
    schema: './src/database/Schema.ts',
    dialect: 'postgresql',
    dbCredentials: { url: process.env.DB_URL ?? '' },
});

and I confirmed process.env.DB_URL is correct by adding a console.log(process.env.DB_URL) to drizzle.config.ts.

Any idea what could be causing this?

Share Improve this question asked Feb 7 at 18:40 NULL pointerNULL pointer 1,3771 gold badge17 silver badges31 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

I found the problem and thought I would share the answer.

This solution is mac specific, hence the question title includes 'on mac'

There was a .DS_Store file in my src/database/migrations/meta/ folder. As .gitignore (correctly) contains a line for *.DS_Store, it was unique to my local files and not in the repo, hence "it works on my machine" was the retort of colleagues. .DS_Store does not show up in finder, even with shift-command-period 'show hidden files' turned on, but I added console.log(snapshots) in node_modules/drizzle-kit/bin.cjs line 6951:

node_modules/drizzle-kit/bin.cjs

...
...
validateWithReport = (snapshots, dialect4) => {
  const { validator: validator2, version: version3 } = validatorForDialect(dialect4);
  console.log(info(`Validating ${snapshots.length} snapshots`));
  console.log(snapshots);
  const result = snapshots.reduce(
    (accum, it) => {
      const raw2 = JSON.parse((0, import_fs.readFileSync)(`./${it}`).toString());
      accum.rawMap[it] = raw2;
      if (raw2["version"] && Number(raw2["version"]) > version3) {
...
...

and it output the expected files, with an extra, BONUS .DS_Store file:

john@Johns-MacBook-Pro repo % yarn db:generate Test
yarn run v1.22.22
$ drizzle-kit generate --config=drizzle.config.ts --name Test
Reading config file '/Users/john/Documents/code/repo/drizzle.config.ts'
Info: Validating 7 snapshots
[
  'src/database/migrations/meta/.DS_Store',
  'src/database/migrations/meta/0000_snapshot.json',
  'src/database/migrations/meta/0001_snapshot.json',
  'src/database/migrations/meta/0002_snapshot.json',
  'src/database/migrations/meta/0003_snapshot.json',
  'src/database/migrations/meta/0004_snapshot.json',
  'src/database/migrations/meta/0005_snapshot.json'
]

so I did rm src/database/migrations/meta/.DS_Store and the drizzle generate worked.

PS. You can run find . -name '.DS_Store' -type f to see what .DS_Store files exist in a subtree, and find . -name '.DS_Store' -type f -delete to delete them.

发布评论

评论列表(0)

  1. 暂无评论