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

"Failed to load env from …\.env TypeError: Cannot read properties of undefined (reading 'split')" and "Module not found: Can't resolve '@builder.io/partytown/integration' in …" errors #35645

Closed
1 task done
omareg94 opened this issue Mar 28, 2022 · 10 comments
Labels
bug Issue was opened via the bug report template. please add a complete reproduction The issue lacks information for further investigation Script (next/script) Related to Next.js Script Optimization.

Comments

@omareg94
Copy link

omareg94 commented Mar 28, 2022

Verify canary release

  • I verified that the issue exists in Next.js canary release

Provide environment information

Operating System:
Platform: win32
Arch: x64
Version: Windows 10 Enterprise
Binaries:
Node: 16.14.0
npm: N/A
Yarn: N/A
pnpm: N/A
Relevant packages:
next: 12.1.2-canary.0
react: 17.0.2
react-dom: 17.0.2

How are you deploying your application? (if relevant)

npx cross-env DEBUG=server:* node server.js

Describe the Bug

When I run npx cross-env DEBUG=server:* node server.js using next latest stable release 12.1.1 or latest canary release 12.1.2-canary.0, I get:

info  - Loaded env from C:\my-app\.env.local
error - Failed to load env from C:\my-app\.env TypeError: Cannot read properties of undefined (reading 'split')
    at C:\my-app\node_modules\@next\env\dist\index.js:1:311
    at Array.reduce (<anonymous>)
    at _interpolate (C:\my-app\node_modules\@next\env\dist\index.js:1:125)
    at expand (C:\my-app\node_modules\@next\env\dist\index.js:1:709)
    at processEnv (C:\my-app\node_modules\@next\env\dist\index.js:1:3638)
    at Module.loadEnvConfig (C:\my-app\node_modules\@next\env\dist\index.js:1:4443)
    at Object.loadConfig [as default] (C:\my-app\node_modules\next\dist\server\config.js:42:21)
    at NextServer.loadConfig (C:\my-app\node_modules\next\dist\server\next.js:114:48)
    at NextServer.getServer (C:\my-app\node_modules\next\dist\server\next.js:120:39)
    at NextServer.prepare (C:\my-app\node_modules\next\dist\server\next.js:96:35)
warn  - SWC minify release candidate enabled. https://nextjs.org/docs/messages/swc-minify-enabled
event - compiled client and server successfully in 14.4s (174 modules)
  server:main > Ready on http://localhost:3000 +0ms
GET / 307 - - 18.663 ms
wait  - compiling / (client and server)...
warn  - ./node_modules/next/dist/pages/_document.js
Module not found: Can't resolve '@builder.io/partytown/integration' in 'C:\my-app\node_modules\next\dist\pages'

However, on next previous stable release 12.1.0, it works normally without any of these warnings.

Expected Behavior

App to work normally without either of these warnings:
error - Failed to load env from C:\my-app.env TypeError: Cannot read properties of undefined (reading 'split')
warn - ./node_modules/next/dist/pages/_document.js
Module not found: Can't resolve '@builder.io/partytown/integration' in 'C:\my-app\node_modules\next\dist\pages'

To Reproduce

Use .env file, and custom _document.js on stable release 12.1.1 or canary release 12.1.2-canary.0.

@omareg94 omareg94 added the bug Issue was opened via the bug report template. label Mar 28, 2022
@balazsorban44 balazsorban44 added the Script (next/script) Related to Next.js Script Optimization. label Mar 28, 2022
@balazsorban44
Copy link
Member

Are you using the recently introduced Script with an experimental worker strategy?

Would you mind attaching a reproduction/your code? 🙏

@ijjk ijjk added the please add a complete reproduction The issue lacks information for further investigation label Mar 28, 2022
@gentlementlegen
Copy link
Contributor

Ran into the same issue. Easily reproduced with an .env file containing $# in the values:
MY_VAR="bug$#bug"

@housseindjirdeh
Copy link
Collaborator

@omareg94 I can't seem to reproduce this issue locally, so like Balázs mentioned - please share a reproduction :)

@FernandVEYRIER Are you seeing Module not found: Can't resolve '@builder.io/partytown/integration' in your error message or only TypeError: Cannot read property 'split' of undefined?

kodiakhq bot pushed a commit that referenced this issue Mar 31, 2022
…#35793)

A few errors about Next.js not correctly handling the missing partytown dependency in `_document.js` have been flagged. Instead of `/* webpackIgnore: true */`, this PR switches to use [`__non_webpack_require__`](https://webpack.js.org/api/module-variables/#__non_webpack_require__-webpack-specific) to ensure webpack doesn't process fetching the dep server-side. 

-----

Related issues: 

- #35645 
- #35630 (comment)
@gentlementlegen
Copy link
Contributor

Hello @housseindjirdeh ! Thanks for the quick reply.

error - Failed to load env from /.env.local TypeError: Cannot read properties of undefined (reading 'split')
    at /node_modules/@next/env/dist/index.js:1:311
    at Array.reduce (<anonymous>)
    at _interpolate (/node_modules/@next/env/dist/index.js:1:125)
    at expand (/node_modules/@next/env/dist/index.js:1:709)
    at processEnv (/node_modules/@next/env/dist/index.js:1:3638)
    at Module.loadEnvConfig (/node_modules/@next/env/dist/index.js:1:4443)
    at Object.loadConfig [as default] (/node_modules/next/dist/server/config.js:42:21)
    at NextServer.loadConfig (/node_modules/next/dist/server/next.js:114:48)
    at NextServer.getServer (/node_modules/next/dist/server/next.js:120:39)
    at NextServer.prepare (/node_modules/next/dist/server/next.js:96:35)

Here is the whole dump of the only error on start

@marlon307
Copy link

I'm also facing the same problem after updating my REACT version to 18

@sdeagh
Copy link

sdeagh commented Apr 1, 2022

My steps to recreate:

  1. yarn create next-app testsite
  2. cd testsite
  3. yarn build (worked fine)
  4. create blank file .env.local
  5. yarn build (worked fine)
  6. add an environment variable, e.g., MY_TEST_ENV_VARIABLE="hello"
  7. yarn build (worked fine)
  8. edit environment variable to include a $ symbol e.g., MY_TEST_ENV_VARIABLE="hello$"
  9. yarn build now results in:

error - Failed to load env from /Users/******/Dev/testsite/.env.local TypeError: Cannot read properties of undefined (reading 'split')

**EDIT: READING THE DOCS - The $ symbol now needs to be escaped i.e. MY_TEST_VARIABLE="hello\$"**

@omareg94
Copy link
Author

omareg94 commented Apr 1, 2022

For Failed to load env from …\.env TypeError: Cannot read properties of undefined (reading 'split') error :

I found that I should have escaped the dollar sign (e.g. MY_VAR="AA\$^A" or MY_VAR="bug\$#bug") (Not a bug in Next release).
The error is triggered because of a value containing unescaped dollar sign followed by special character (e.g. $^ or $#) like in MY_VAR="AA$^A" and MY_VAR="bug$#bug" (@FernandVEYRIER's). These are invalid environment values because no environment variable name can contain ^ or # characters, so $ need to be escaped (i.e. .env parser expects $^A and $#bug to be a call to existing variables if $ not escaped).

For the Module not found: Can't resolve '@builder.io/partytown/integration' in 'C:\my-app\node_modules\next\dist\pages error:

I'm trying to figure out the reproducing set, as I can't post the code as is.

@housseindjirdeh
Copy link
Collaborator

Thanks for the details everyone. Looks like there are two separate issues here:

  • Failed to load env from …\.env TypeError: Cannot read properties of undefined (reading 'split'): Like some of you have already noticed, you need to escape the $ character (see note in docs)
  • Module not found: Can't resolve '@builder.io/partytown/integration': This may be due to a setup issue, but I've just included a patch for webpack to not parse the require of the package server-side which should remove any likelihood of it erroring due to not being resolved. This should prevent this error from popping up when it shouldn't :)

@housseindjirdeh
Copy link
Collaborator

Closing this for now! @omareg94 if you are still running into the issue, please update your next dep to v12.1.5-canary.0 as that should fix the problem.

MH4GF added a commit to MH4GF/log.mh4gf.dev that referenced this issue Apr 7, 2022
…egration' in …"

yarn devで上記のエラーが出る。
このissueが同様の問題だった。 vercel/next.js#35645
プロジェクト側の問題かもしれないが、next側でthrowする必要はなさそうなため治したとのこと。
canaryバージョンではあるが、上げることによって修正を取り込み解決した。
@github-actions
Copy link
Contributor

github-actions bot commented May 2, 2022

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 2, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template. please add a complete reproduction The issue lacks information for further investigation Script (next/script) Related to Next.js Script Optimization.
Projects
None yet
Development

No branches or pull requests

7 participants