Home
Support Forums

Problem running a script before netlify/build process (not before the build command)

UPTADE
Updated title from Netlify.toml written at npm "postinstall" not being detected by build engine fro clarity

Hi team,

PROBLEM
In my current use case, I am trying to write a custom netlify.toml before each build. There are multiple reasons why, but the most important reasons are:

  • I cant track netlify.toml on git (this is on purpose)
  • I want to make sure end-users don’t change or overwrite our netlify.toml
  • Our netlify.toml have custom build plugins

ATTEMPED SOLUTION
I have configured a "postinstall" script which executes the command:
node --eval "require('@myprivateorg/netlify-toml')"

This command effectively writes a netlify.toml to the root of the project. This works perfectly fine locally.

Unfortunately, despite the fact that the command executes successfully after the project dependencies are installed , it appears that it’s somehow written too late for the netlify build engine to detect it. i.e our netlify.toml config is being ignored and our custom plugins are not being loaded.

PROBLEM LOG

7:39:51 AM: [5/5] Building fresh packages...
7:39:57 AM: $ node --eval "require('@myorg/netlify-toml')"
7:39:59 AM: Created default netlify.toml at /opt/build/repo <------ βœ…
7:39:59 AM: Done in 61.65s.
7:39:59 AM: NPM modules installed using Yarn
7:39:59 AM: Started restoring cached go cache
7:39:59 AM: Finished restoring cached go cache
7:39:59 AM: go version go1.14.4 linux/amd64
7:39:59 AM: go version go1.14.4 linux/amd64
7:39:59 AM: Installing missing commands
7:39:59 AM: Verify run directory
7:40:00 AM: ​
7:40:00 AM: ────────────────────────────────────────────────────────────────
7:40:00 AM:   Netlify Build                                                 
7:40:00 AM: ────────────────────────────────────────────────────────────────
7:40:00 AM: ​
7:40:00 AM: ❯ Version
7:40:00 AM:   @netlify/build 17.9.2
7:40:00 AM: ​
7:40:00 AM: ❯ Flags
7:40:00 AM:   baseRelDir: true
7:40:00 AM:   deployId: 61128f70de851d43819b2cb8
7:40:00 AM: ​
7:40:00 AM: ❯ Current directory
7:40:00 AM:   /opt/build/repo
7:40:00 AM: ​
7:40:00 AM: ❯ Config file <--- 🚫 Not detecting the netlify.toml we just wrote
7:40:00 AM:   No config file was defined: using default values.
7:40:00 AM: ​
7:40:00 AM: ❯ Context
7:40:00 AM:   production
7:40:00 AM: ​
7:40:00 AM: ❯ Loading plugins <---- 🚫 Not detecting our netlify.toml build plugins
7:40:00 AM:    - @netlify/plugin-nextjs@3.8.0 from Netlify app and package.json 7:40:00 AM:  <--- ⚠️ running because its also added via the UI but ignoring our version

EXPECTED RESULT
2:15:54 PM: ❯ Loading plugins
2:15:54 PM: - @netlify/plugin-nextjs@3.8.0 from netlify.toml and package.json
2:15:54 PM: - @myorg/netlify-xx@0.0.105 from netlify.toml and package.json
2:15:54 PM: - @myorg/netlify-yy@0.0.105 from netlify.toml and package.json

FULL DEPLOYMENT LOGS
Full failing deploy logs @ deploy id 6112981897b0290007963e38


Am i doing this wrong?

Is there a way to run a script before the netlify build engine kicks in?

Thanks for your help
JP

As a test, I just attempted to copy a netlify.toml from a committed subfolder on preinstall without any luck either. i.e the copied .toml is still not being detected

2:05:24 AM: Finished restoring cached yarn cache
2:05:25 AM: No yarn workspaces detected
2:05:25 AM: Started restoring cached node modules
2:05:25 AM: Finished restoring cached node modules
2:05:25 AM: Installing NPM modules using Yarn version 1.22.10
2:05:25 AM: yarn install v1.22.10
2:05:25 AM: info No lockfile found.
2:05:25 AM: $ cp  ./netlify-toml/netlify.toml ./netlify.toml <---- βœ…
2:05:25 AM: [1/5] Validating package.json...
2:05:25 AM: [2/5] Resolving packages...
...
uuid@3.4.0: Please upgrade  to version 7 or higher.  Older vers

later on the logs

2:06:16 AM: ❯ Current directory
2:06:16 AM:   /opt/build/repo
2:06:16 AM: ​
2:06:16 AM: ❯ Config file
2:06:16 AM:   No config file was defined: using default values. <---- 😒
2:06:16 AM: ​
2:06:16 AM: ❯ Context
2:06:16 AM:   production
2:06:16 AM: ​
2:06:16 AM: ❯ Loading plugins
2:06:16 AM:    - @netlify/plugin-nextjs@3.8.0 from Netlify app and package.json
2:06:17 AM: 

Again the goal is to use a netlify.toml without it being tracked on the repo.

Thanks for the help!
JP

Hi @jpprietobaez,

netlify.toml is accessed before Node Modules are even installed. So, this won’t work sadly. If the file is not present when it’s tried to be accessed, it’s assumed the file doesn’t exist and is ignored till the end of the command.

:frowning:

Is there any other buildbot hook, β€œwhen it’s tried to be accessed” that I can hook into to achieve this?

Or is there any other way to force bulilds to use a particular netlify.toml that’s not in the root of the project?

Hi @jpprietobaez,

I’m afraid that’s something not possible at the moment. This could be added as a feature request, but I’m not sure if/when this would be implemented.

mmmm, that’s a shame.

This definitely limits our ability to simplify our product offering. Our netlify.toml is fairly long and complex, so we would love to abstract it away, so that endusers don’t have to worry about the inners workings of the product.

Would be great to have this added as a feature request.

I created a github issue @ netlify.toml written at "postinstall" not detected by @netlify/build Β· Issue #3413 Β· netlify/build Β· GitHub

Thanks again for the clarification @hrishikesh

An alternative way would be to generate the required TOML file using GitHub Actions or something similar and push a commit with it. Maybe this commit can go to a different branch and you can set that branch as the production one in Netlify.

Not ideal, but might work.

1 Like

For those in the future theres more info on this topic here