Build fails with prebuild requiring node script - Exit code 2

12:36:39 AM: $ yarn build
12:36:39 AM: yarn run v1.22.4
12:36:39 AM: $ node -e require('./src/buildPosts.js').getPosts()
12:36:39 AM: /bin/sh: 1: Syntax error: "(" unexpected
12:36:39 AM: error Command failed with exit code 2.

I am attempting to run a node script during the prebuild process that will generate a json file in my src directory. It works locally however, when ran in the netlify build shell it seems to complain about bin/sh: 1: Syntax error: "(" unexpected, when googling the error most responces relate back to bash scripting, is this command not suppose to be using “bin/sh” somehow?

I am not sure of the root cause, is it because the build is running in ubuntu and thats different to my bash terminal? Or maybe I am just using the wrong syntax for the netlify build?

Buildlogs here: https://app.netlify.com/sites/cocky-pasteur-345a6f/deploys/5fb860c3368c370007cba681

Ok. I do get this in ubuntu running node v14.15.0

Probably not a good idea to use node -e to eval the script in the cli

Try creating a script file instead and run node ./pre-build.js

pre-build.js

const buildPosts = require("./src/buildPosts");

buildPosts.getPosts();

in package.json:
"prebuild": "node ./pre-build.js",

AH yes! That’s it, the mistake has come from my misunderstanding of running the node script. Thanks a lot Talves!

1 Like