How to access an existing environment variable in blueprint

I’m trying to access the npm_package_version existing environment variable in my Next.js app client through my blueprint. I’ve tried this kind of thing:

      - key: NEXT_PUBLIC_APP_VERSION
        envVarKey: npm_package_version

and

      - key: NEXT_PUBLIC_APP_VERSION
        value: $npm_package_version

and (from itself)

      - key: NEXT_PUBLIC_APP_VERSION
        fromService:
          type: web
          name: my_nextjs
          envVarKey: npm_package_version

but it’s not valid syntax.

Any tips?

Hi Sean,

Thanks for reaching out.

Are you referring to the npm_package_version that’s available in NPM scripts? That wouldn’t be available to a Blueprint, as it’s not a “true” environment variable and is only available in the context of Node through NPM. For example, in one of my personal apps running Node:

There’s no env var called npm_package_version:

$ echo $npm_package_version

Which means it’s also not available in plain Node:

$ nodeWelcome to Node.js v14.19.1.Type ".help" for more information.> process.env.npm_package_versionundefined

Or a file logging it:

$ cat test_env.js console.log(process.env.npm_package_version)$ node test_env.js undefined

However, if that same script is accessed through NPM, it will return a value:

$ cat package.json { "version": "1.0.0", "scripts": { "foo": "node test_env.js" }}$ npm run foo> @1.0.0 foo /opt/render/project/src> node test_env.js1.0.0

You may need to find another way to set that env var, which may be tricky if you’re looking to do it dynamically.

Depending on where you’re intending to use the env var, maybe setting it as part of the build or start command would be sufficient? For example, using the same package.json above:

$ export NEXT_PUBLIC_APP_VERSION=`cat package.json | jq -r .version`$ echo $NEXT_PUBLIC_APP_VERSION1.0.0

Hope that helps

Kind regards

Alan

Thanks Alan.

I can write a start script for that one yes.

Just as an aside though, because Next.js front end needs its environment vars to start with NEXT_PUBLIC in order to be accessible, is there no way to dynamically re-create “true” environment variables with this prefix without a script? Eg:

      - key: NEXT_PUBLIC_URL
        envVarKey: RENDER_EXTERNAL_URL

Thanks
Sean

Not currently, however there is a feature request on our feedback site for this type of functionality. https://feedback.render.com/features/p/environment-variable-string-interpolation

Please feel free to upvote/comment so that you’re notified of any updates.

Alan

Thanks Alan.

For anyone else… tiny init.sh shell script works for me to get these environment vars into Next.js:

#!/bin/bash

packageVersion=$(jq -r .version package.json)
lastCommitChars=${RENDER_GIT_COMMIT:(-6)}
nextUrl=${RENDER_EXTERNAL_URL}
echo "Package Version: $packageVersion"
echo "Git Last Commit: $lastCommitChars"
echo "Next Url: $nextUrl" 
export NEXT_PUBLIC_APP_VERSION=$packageVersion
export NEXT_PUBLIC_GIT_COMMIT=$lastCommitChars
export NEXT_PUBLIC_URL=$nextUrl

And then for Next.js build command:
cd www && yarn && source init.sh && yarn build

:+1:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.