Next.js Discord

Discord Forum

Next CLI as a package

Answered
Tosa posted this in #help-forum
Open in Discord
TosaOP
Is it possible to use the next.js CLI as a npm package? Instead of using spawn in node is there a way to require next and call the commands it as a function?
Answered by joulev
ah that's what you meant. in that case there is no existing guide on that, but why don't you simply
{
  "scripts": {
-   "build": "next build"
+   "build": "node prebuild.js && "next build"
  }
}

or have a prebuild script
View full answer

6 Replies

TosaOP
Thanks. Apologies for the confusion I meant for building. I have a build.js node script that prepares all the env variable, config, etc. Was just wondering if I can initiate the next build from within node without having to rely on spawn.
@Tosa Thanks. Apologies for the confusion I meant for building. I have a `build.js` node script that prepares all the env variable, config, etc. Was just wondering if I can initiate the `next build` from within node without having to rely on spawn.
ah that's what you meant. in that case there is no existing guide on that, but why don't you simply
{
  "scripts": {
-   "build": "next build"
+   "build": "node prebuild.js && "next build"
  }
}

or have a prebuild script
Answer
it's quite common to have something like node prebuild.js && next build as well, e.g. if you use prisma you will do prisma generate && next build
so it's not an anti pattern or something like that, you can run anything before or after the build and it is perfectly fine
TosaOP
👍 Okay, thanks for that.