Next.js Discord

Discord Forum

Run a ts file in prebuild

Answered
Brouillard posted this in #help-forum
Open in Discord
I want to run some command before building the app (basicaly it check some md file and add some entry in the db if necessary).
But I issues with import/require, I was wandering what would be the right way to do it.

here is the command btw :
import fs from "fs"
import path from "path"
import matter from "gray-matter"
import event from "../app/api/events/model"

const eventDir = path.join(process.cwd(), "src/content/events")

fs.readdir(eventDir, (err, files) => {
  if (err) {
    console.error("Failed to list files in directory:", err)
    return
  }

  files.forEach(async (file) => {
    const filePath = path.join(eventDir, file)
    const fileContent = fs.readFileSync(filePath, "utf8")
    const { data, content } = matter(fileContent)

    if (data.id) {
      const existingEvent = await event.getEvent(data.id)
      if (existingEvent) return
    }

    const newEvent = await event.createEvent(data)
    if (!data.id) {
      const newEventId = newEvent.id
      data.id = newEventId
      const newFileContent = matter.stringify(content, data)
      fs.writeFileSync(filePath, newFileContent)
    }
  })
})

31 Replies

sounds nice, could you tell me more about it ?
ha you mean instead of using typescript ?
yes. // @ts-check and jsdoc already provides the validation and typing
i recommend using typescript where possible, but in this case typescript is just a maintenance overhead and requires effort in maintaining a complex workflow so i don't recommend it
but the project is already setup I would have to redo it all again
this kind :
SyntaxError: Cannot use import statement outside a module

the problem is that if I use require isntead the "event" file that is being import contains import etc...
how do you run the file?
ts-node
pnpm ts-node src/utils/additional.build.ts
@Brouillard ts-node
okay
> pp ts-node-esm src/utils/additional.build.ts
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /home/brouillard/Documents/PROG/rencontres_artistiques/src/utils/additional.build.ts
    at new NodeError (node:internal/errors:399:5)
    at Object.getFileProtocolModuleFormat [as file:] (node:internal/modules/esm/get_format:99:9)
    at defaultGetFormat (node:internal/modules/esm/get_format:139:38)
    at defaultLoad (node:internal/modules/esm/load:83:20)
    at nextLoad (node:internal/modules/esm/hooks:735:28)
    at load (/home/brouillard/Documents/PROG/rencontres_artistiques/node_modules/.pnpm/ts-node@10.9.1_@types+node@20.8.6_typescript@5.2.2/node_modules/ts-node/dist/child/child-loader.js:19:122)
    at nextLoad (node:internal/modules/esm/hooks:735:28)
    at Hooks.load (node:internal/modules/esm/hooks:380:26)
    at MessagePort.handleMessage (node:internal/modules/esm/worker:165:24)
    at [nodejs.internal.kHybridDispatch] (node:internal/event_target:762:20) {
  code: 'ERR_UNKNOWN_FILE_EXTENSION'
}
weird
Answer
you can downgrade to node 18 or use [node --no-warnings=ExperimentalWarning --loader ts-node/esm file.ts](https://github.com/TypeStrong/ts-node/issues/1997#issuecomment-1537111441)
thanks a lot I'm trying this
I have the same error
SyntaxError: Cannot use import statement outside a module
the issue has a few more workarounds, try them
yes I'm doing that
thanks
if able you can also use bun instead of node
if no workarounds work and you can't use bun you probably have to downgrade to node 18
which is still in LTS so still fine (for now)
okay
thanks a lot