Skip to content

Conversation

darrenkidd
Copy link
Contributor

Getting this warning over and over again in my Next.JS project:

It looks like you're using the browser version of the SDK in a node.js environment. You should require('parse/node') instead.

Was driving me a bit crazy, as I most certainly do have SERVER_RENDERING=1 set for my app. I've confirmed this via console.log() directly before the Parse.Initialize() call.

image

Looking at my ./node_modules/parse/lib/browser/Parse.js shows this (odd) line:

if ("browser" === 'browser' && _CoreManager.default.get('IS_NODE') && !undefined) {
  ...

Given that it's most definitely being written correctly here, I was able to track it down to the slightly misconfigured env transform plugin you'll see in the diff.

Have confirmed that this is the only variable that needs to be excluded from the 3 different build types (PARSE_BUILD is the main target for this inline replacement):

[email protected]:~/environment/Parse-SDK-JS (master) $ grep -r process.env. . --exclude-dir=node_modules
./gulpfile.js:var BUILD = process.env.PARSE_BUILD || 'browser';
./src/ParseFile.js:    } else if (process.env.PARSE_BUILD === 'node') {
./src/__tests__/ParseFile-test.js:    process.env.PARSE_BUILD = 'node';
./src/__tests__/ParseFile-test.js:    process.env.PARSE_BUILD = 'browser';
./src/EventEmitter.js:if (process.env.PARSE_BUILD === 'react-native') {
./src/Parse.js:    if (process.env.PARSE_BUILD === 'browser' && CoreManager.get('IS_NODE') && !process.env.SERVER_RENDERING) {
./src/Parse.js:if (process.env.PARSE_BUILD === 'node') {
./src/LiveQueryClient.js:    if (process.env.PARSE_BUILD === 'node') {
./src/LiveQueryClient.js:    } else if (process.env.PARSE_BUILD === 'browser') {
./src/LiveQueryClient.js:    } else if (process.env.PARSE_BUILD === 'react-native') {
./src/LocalDatastore.js:if (process.env.PARSE_BUILD === 'react-native') {
./src/LocalDatastore.js:} else if (process.env.PARSE_BUILD === 'browser') {
./src/RESTController.js:if (process.env.PARSE_BUILD === 'node') {
./src/Storage.js:if (process.env.PARSE_BUILD === 'react-native') {
./src/Storage.js:} else if (process.env.PARSE_BUILD === 'browser') {

All tests pass as expected - I didn't touch any actual code.

Here's the full run-down to prove the fix works:

[email protected]:~/environment/Parse-SDK-JS (master) $ sed -n 34p src/Parse.js
    if (process.env.PARSE_BUILD === 'browser' && CoreManager.get('IS_NODE') && !process.env.SERVER_RENDERING) {
[email protected]:~/environment/Parse-SDK-JS (master) $ npm run build

> [email protected] build /home/ec2-user/environment/Parse-SDK-JS
> ./build_releases.sh

Building JavaScript SDK v2.5.1...\n
Cleaning up old builds...\n
Browser Release:
[10:23:38] Using gulpfile ~/environment/Parse-SDK-JS/gulpfile.js
[10:23:38] Starting 'compile'...
[10:23:46] Finished 'compile' after 8.06 s
Node.js Release:
[10:23:46] Using gulpfile ~/environment/Parse-SDK-JS/gulpfile.js
[10:23:46] Starting 'compile'...
[10:23:51] Finished 'compile' after 4.92 s
React Native Release:
[10:23:52] Using gulpfile ~/environment/Parse-SDK-JS/gulpfile.js
[10:23:52] Starting 'compile'...
[10:23:56] Finished 'compile' after 3.49 s
Bundling and minifying for CDN distribution:
[10:23:56] Using gulpfile ~/environment/Parse-SDK-JS/gulpfile.js
[10:23:56] Starting 'browserify'...
[10:23:57] Finished 'browserify' after 1.3 s
[10:23:59] Using gulpfile ~/environment/Parse-SDK-JS/gulpfile.js
[10:23:59] Starting 'minify'...
[10:24:03] Finished 'minify' after 4.21 s
[email protected]:~/environment/Parse-SDK-JS (master) $ sed -n 50p lib/browser/Parse.js
    if ("browser" === 'browser' && _CoreManager.default.get('IS_NODE') && !undefined) {
[email protected]:~/environment/Parse-SDK-JS (master) $ diff gulpfile.js fixed-gulpfile.js
32c32,33
<   'browser': [transformRuntime, '@babel/plugin-transform-flow-comments', '@babel/plugin-proposal-class-properties', 'inline-package-json', 'transform-inline-environment-variables'],
---
>   'browser': [transformRuntime, '@babel/plugin-transform-flow-comments', '@babel/plugin-proposal-class-properties', 'inline-package-json',
>     ['transform-inline-environment-variables', {'exclude': ['SERVER_RENDERING']}]],
[email protected]:~/environment/Parse-SDK-JS (master) $ mv fixed-gulpfile.js gulpfile.js
[email protected]:~/environment/Parse-SDK-JS (master) $ npm run build

> [email protected] build /home/ec2-user/environment/Parse-SDK-JS
> ./build_releases.sh

Building JavaScript SDK v2.5.1...\n
Cleaning up old builds...\n
Browser Release:
[10:24:43] Using gulpfile ~/environment/Parse-SDK-JS/gulpfile.js
[10:24:43] Starting 'compile'...
[10:24:51] Finished 'compile' after 8 s
Node.js Release:
[10:24:52] Using gulpfile ~/environment/Parse-SDK-JS/gulpfile.js
[10:24:52] Starting 'compile'...
[10:24:57] Finished 'compile' after 4.76 s
React Native Release:
[10:24:57] Using gulpfile ~/environment/Parse-SDK-JS/gulpfile.js
[10:24:57] Starting 'compile'...
[10:25:01] Finished 'compile' after 3.54 s
Bundling and minifying for CDN distribution:
[10:25:02] Using gulpfile ~/environment/Parse-SDK-JS/gulpfile.js
[10:25:02] Starting 'browserify'...
[10:25:03] Finished 'browserify' after 1.36 s
[10:25:04] Using gulpfile ~/environment/Parse-SDK-JS/gulpfile.js
[10:25:04] Starting 'minify'...
[10:25:08] Finished 'minify' after 4.26 s
[email protected]:~/environment/Parse-SDK-JS (master) $ sed -n 50p lib/browser/Parse.js
    if ("browser" === 'browser' && _CoreManager.default.get('IS_NODE') && !process.env.SERVER_RENDERING) {

I haven't created an issue but I'm happy to do so if needed.

Related:

@codecov
Copy link

codecov bot commented Jul 22, 2019

Codecov Report

Merging #873 into master will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##           master     #873   +/-   ##
=======================================
  Coverage   92.14%   92.14%           
=======================================
  Files          54       54           
  Lines        5006     5006           
  Branches     1121     1121           
=======================================
  Hits         4613     4613           
  Misses        393      393

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 7e805e0...2c295a9. Read the comment docs.

Copy link
Member

@davimacedo davimacedo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@darrenkidd thanks for the PR! It makes sense to me. @dplewis do you also want to take a look?

@dplewis
Copy link
Member

dplewis commented Jul 22, 2019

In your test log I don’t see SERVER_RENDERING=1 npm run build. What would that output? Sorry I’m not near a computer to test

@darrenkidd
Copy link
Contributor Author

Hey @dplewis,

In your test log I don’t see SERVER_RENDERING=1 npm run build. What would that output? Sorry I’m not near a computer to test

Not sure what that's testing, as SERVER_RENDERING is a runtime env var, not a build time env var. But here's the output. Is this what you're after?

[email protected]:~/environment/Parse-SDK-JS (fix/server-rendering-env-var) $ SERVER_RENDERING=1 npm run build

> [email protected] build /home/ec2-user/environment/Parse-SDK-JS
> ./build_releases.sh

Building JavaScript SDK v2.5.1...\n
Cleaning up old builds...\n
Browser Release:
[19:50:09] Using gulpfile ~/environment/Parse-SDK-JS/gulpfile.js
[19:50:09] Starting 'compile'...
[19:50:18] Finished 'compile' after 8.99 s
Node.js Release:
[19:50:18] Using gulpfile ~/environment/Parse-SDK-JS/gulpfile.js
[19:50:18] Starting 'compile'...
[19:50:24] Finished 'compile' after 5.37 s
React Native Release:
[19:50:24] Using gulpfile ~/environment/Parse-SDK-JS/gulpfile.js
[19:50:24] Starting 'compile'...
[19:50:28] Finished 'compile' after 3.81 s
Bundling and minifying for CDN distribution:
[19:50:29] Using gulpfile ~/environment/Parse-SDK-JS/gulpfile.js
[19:50:29] Starting 'browserify'...
[19:50:30] Finished 'browserify' after 1.37 s
[19:50:32] Using gulpfile ~/environment/Parse-SDK-JS/gulpfile.js
[19:50:32] Starting 'minify'...
[19:50:36] Finished 'minify' after 4.6 s

Let me know if you need anything else.

Copy link
Member

@dplewis dplewis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just tried and it looks good.

@dplewis dplewis merged commit c64ca1d into parse-community:master Jul 22, 2019
@TomWFox
Copy link
Contributor

TomWFox commented Jul 22, 2019

@darrenkidd thanks for contributing!

@darrenkidd
Copy link
Contributor Author

@darrenkidd thanks for contributing!

No worries, happy to help! Will see if there are some other tasks to pick off when I get time.

@darrenkidd darrenkidd deleted the fix/server-rendering-env-var branch July 22, 2019 21:21
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

Successfully merging this pull request may close these issues.

4 participants