site: https://dev--keen-ritchie-2146a3.netlify.app
I’m very new to netlify and nextjs for that matter. I’m managing to get everything working and in general not having in real issues with build/deploy. I am building some api’s using nextjs (v11).
I am trying to figure out how I can determine what context the api is running in (dev, branch-deploy, production). I am currently on the free tier. I see in the docs there is a CONTEXT environment variable. This looks to be exactly what I’d like to get to but I can’ t figure out how.
I’ve tried to use process.env.CONTEXT in my nextjs api endpoint. I’ve been using console.logs in various points to try and see what the value is at various points. It always seems to be undefined. What am I missing?
Thanks in advance,
Bob
CONTEXT is a build-time variable, that is, it’s not available to functions. You might have to use this:
Thank you for the response. More newbie issues trying to get plugin working…
I did not have a netlify.toml file in my project. I had configured everything in the web ui. I’ve added the following to the netlify.toml file at the root of my project.
[[plugins]]
package = "netlify-plugin-inline-functions-env"
[plugins.inputs]
include = ["CONTEXT"]
This is the only entry in the file.
When I build I am getting the following error:
7:23:35 AM: ────────────────────────────────────────────────────────────────
7:23:35 AM: Plugin "netlify-plugin-inline-functions-env" failed
7:23:35 AM: ────────────────────────────────────────────────────────────────
7:23:35 AM:
7:23:35 AM: Error message
7:23:35 AM: Error: Failed to inline function files because netlify function folder was not configured or pointed to a wrong folder, please check your configuration
I have tried numerous things but I am not able to get the nextjs project to build. If I remove netlify.toml the project builds/runs with no errors.
Any help greatly appreciated.
Bob
Do you have a functions directory setup in netlify.toml?
It should look like:
[functions]
directory = "your functions folder"
[[plugins]]
package = "netlify-plugin-inline-functions-env"
[plugins.inputs]
include = ["CONTEXT"]
I’ve changed my netlify.toml to this with the same result. I removed the plugins.inputs to get things as simple as possible.
[functions]
directory = "netlify/functions"
[[plugins]]
package = "netlify-plugin-inline-functions-env"
And the error is still the same?
It appears to be the same.
10:57:13 AM: ────────────────────────────────────────────────────────────────
10:57:13 AM: Plugin "netlify-plugin-inline-functions-env" failed
10:57:13 AM: ────────────────────────────────────────────────────────────────
10:57:13 AM:
10:57:13 AM: Error message
10:57:13 AM: Error: Failed to inline function files because netlify function folder was not configured or pointed to a wrong folder, please check your configuration
Bob
Would it be possible for you to share the repo (or check if you’ve got the correct folder for functions)?
I can do that. Is there a way to private message you the repo url?
PMs should be enabled for your account.
So, if I’m seeing it correctly, your functions live inside helpers/api
folder, correct?
Well, those are functions that are used by other functions. The base functions are at /pages/api.
I was under the impression that the functions build are located in .netlify/functions.
.netlify/functions
is the path for the deployed functions (or functions bundled using the CLI).
You would have your functions’ source code somewhere in the repo, correct?
In nextjs the api functions I’m using start at pages/api. I added in a sub directory “v1” so I guess technically for now the api functions live at /pages/api/v1. When I look at the build logs it looks like those functions end up being built in .netlify/functions directory since I did not specify anything in the functions path.
The application is working as expected so I know that the functions are getting called. But the plugin seems to be confused by where the functions are. Should I set the functions to pages/api/v1 and try again?
Yes, if the files in that folder are being published to /.netlify/functions/
, that’s your functions’ directory. You could add that to the plugin and it should work.
With
[functions]
directory = ".netlify/functions"
[[plugins]]
package = "netlify-plugin-inline-functions-env"
I got same error
12:11:01 PM: ────────────────────────────────────────────────────────────────
12:11:01 PM: Plugin "netlify-plugin-inline-functions-env" failed
12:11:01 PM: ────────────────────────────────────────────────────────────────
12:11:01 PM:
12:11:01 PM: Error message
12:11:01 PM: Error: Failed to inline function files because netlify function folder was not configured or pointed to a wrong folder, please check your configuration
I just tried this and got same error:
[functions]
directory = "pages/api/v1"
[[plugins]]
package = "netlify-plugin-inline-functions-env"
I wanted to leave a post here with my resolution. First I want to thank @hrishikesh for helping me and for being instrumental in getting a working solution. Being a new customer (and on the free plan at the moment) , I was impressed by the level of support I received.
The plugin wasn’t going to be a good solution. What I ended up doing is the following.
Before you do any of this make sure you understand what you are doing and for your own sake, don’t write a secret to your public nextjs directory. I take no responsibility if that happens.
- create a file context.js at the root of the project with these contents:
const fs = require('fs');
fs.writeFileSync(
'public/context.json',
JSON.stringify({ context: process.env.CONTEXT })
);
This will write the CONTEXT environment variable to the public directory.
- create the file public/context.json with the contents below. This is needed when you are running locally. Commit this file.
{ "context": "local" }
-
change your build command to node context.js && npm run build
. This will execute the context.js file writing the value of the CONTEXT environment variable to public/context.json.
-
in your nextjs api server side code (in my case) you can access the value in context.json:
const context = require('public/context.json');
console.log('context :>> ', context);
Now nextjs api’s can distinguish between local, branch-deploy and production. AWESOME!
Cheers!
Bob
Manchester, NH US