question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

server side rendering / import into node was recently broken

See original GitHub issue

Many thanks for antd-img-crop, which I really like and use at https://cocalc.com/config/account/avatar

Importing this package in node used to work:

mkdir tmp
cd tmp
npm init -y
npm install antd-img-crop@4.2.4
echo 'require('antd-img-crop')' > a.js
node a.js

Now it doesn’t:

mkdir tmp2
cd tmp2
npm init -y
npm install antd-img-crop@latest
echo 'require('antd-img-crop')' > a.js
node a.js
/tmp/z3$ node a.js
/private/tmp/z3/node_modules/antd/es/locale-provider/LocaleReceiver.js:1
import _extends from "@babel/runtime/helpers/esm/extends";
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at compileFunction (<anonymous>)
    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1033:15)
    at Module._compile (node:internal/modules/cjs/loader:1069:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (/private/tmp/z3/node_modules/antd-img-crop/dist/antd-img-crop.cjs.js:21:22)

The problem is that something is getting confused between ES modules and CommonJS modules and loading the wrong thing.

It’s nice to have node.js support, since it’s needed for server side rendering, which can come up naturally when using next.js.

Please close this if you don’t want to fix this, and I’ll figure out how to restructure my app to work around this problem or just stick with an old version of antd-img-crop.

Thanks!

Issue Analytics

  • State:closed
  • Created 10 months ago
  • Reactions:1
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
williamsteincommented, Dec 13, 2022

Hi, here is the one commit I made to create the package @cocalc/antd-img-crop above:

https://github.com/sagemathinc/antd-img-crop/commit/b3fef1503439fac3b99b556c42dd8c7c58e37495

It’s what works for me. Basically I replace the commonjs module with a dummy one. I had forgot to push this commit before, so sorry if you looked for it and didn’t find it.

1reaction
williamsteincommented, Dec 2, 2022

I ended up making this https://www.npmjs.com/package/@cocalc/antd-img-crop in case anybody else runs into this issue. After trying several things I gave up and just made the commonjs version a dummy function. At least that doesn’t crash.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Server side rendering: node SyntaxError: Cannot use import ...
Background: I am writing a react app with server-side rendering. ... runs it crashes because node does not seem to be able to...
Read more >
React Server Side Rendering With Node And Express
We are going to develop a very simple application that has two pages: list of all latest paid bills; specific bill's details (added...
Read more >
Demystifying Server Side Rendering: Let's build our own SSR ...
They try to dynamically load the corresponding file inside the pages folder using the name they got from the URL. If they succeed,...
Read more >
A beginner's guide to React Server-Side Rendering (SSR)
In this lesson, we are going to talk about server-side rendering (SSR), ... We will run server/index.js using node which imports the ...
Read more >
Handling the React server hydration mismatch error
How to resolve the server mismatch error when hydrating a shared React component that can be used in client-side only or server-side rendered...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found