Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Environment variables are broken #34

Open
empz opened this issue Oct 1, 2021 · 2 comments
Open

Environment variables are broken #34

empz opened this issue Oct 1, 2021 · 2 comments

Comments

@empz
Copy link

empz commented Oct 1, 2021

Trying to load the access token from an environment variable (as it should be from the beginning instead of hardcoding it), breaks the build.

const Storyblok = new StoryblokClient({
  accessToken: process.env.STORYBLOK_PREVIEW_TOKEN,
  cache: {
    clear: "auto",
    type: "memory",
  },
});

Build throws the following error:

> Build error occurred
TypeError: Cannot read property 'env' of undefined
    at Object.320 (/workspace/nextjs-multilanguage-website/.next/server/pages/404.js:754:62)
    at __webpack_require__ (/workspace/nextjs-multilanguage-website/.next/server/webpack-runtime.js:25:42)
    at Object.125 (/workspace/nextjs-multilanguage-website/.next/server/pages/404.js:686:74)
    at __webpack_require__ (/workspace/nextjs-multilanguage-website/.next/server/webpack-runtime.js:25:42)
    at __webpack_exec__ (/workspace/nextjs-multilanguage-website/.next/server/pages/404.js:861:52)
    at /workspace/nextjs-multilanguage-website/.next/server/pages/404.js:862:28
    at Object.<anonymous> (/workspace/nextjs-multilanguage-website/.next/server/pages/404.js:865:3)
    at Module._compile (internal/modules/cjs/loader.js:1072:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)
    at Module.load (internal/modules/cjs/loader.js:937:32) {
  type: 'TypeError'
}

Looking into .next/server/pages/404 or .next/server/pages/[[...slug]].js we can see that something is messing up the process.env call. Seems like autoprefixer or something. Although I thought that was for CSS only, why is it messing up this simple nodejs code? No idea.

const Storyblok = new (external_storyblok_js_client_default())({
  accessToken: external_autoprefixer_namespaceObject.process.env.STORYBLOK_PREVIEW_TOKEN, // ???
  cache: {
    clear: "auto",
    type: "memory"
  }
});
@empz
Copy link
Author

empz commented Oct 1, 2021

Okay it seems we need to use NEXT_PUBLIC environment variables. Although probably not a good idea to expose this to the browser. Or is it a requirement and doesn't impose any security risk?

@fgiuliani
Copy link
Collaborator

Hey @empz sorry for the late response.
Have you taken a look at the new approach using the new React SDK?

https://www.storyblok.com/tp/add-a-headless-cms-to-next-js-in-5-minutes

https://www.npmjs.com/package/@storyblok/react

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants