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

refresh - SyntaxError: Cannot use import statement outside a module #83

Closed
airxiaotian opened this issue Sep 2, 2021 · 19 comments
Closed

Comments

@airxiaotian
Copy link

airxiaotian commented Sep 2, 2021

Describe the bug
React, next.js, refreshing the page, it occurred. but if entering the page from another page, it doesn't occur

To Reproduce
Steps to reproduce the behavior:

  1. Refresh the page containing the component

Screenshots
image

Desktop (please complete the following information):

  • OS: MacOS
  • Browser chrome

@bumbeishvili
Copy link
Owner

@airxiaotian require won't work, the import will

Can you provide more information about the environment where you plan to use this org chart? Is it node.js?

@airxiaotian
Copy link
Author

React, next.js, refreshing the page, it occurred. but if entering the page from another page, it doesn't occur

@airxiaotian
Copy link
Author

airxiaotian commented Sep 2, 2021

Is it node.js?

yes, node js , by using npm i d3-org-chart

@bumbeishvili
Copy link
Owner

This is react.js integration sample - https://stackblitz.com/edit/d3-org-chart-react-integration-hooks

I can't reproduce it, can you fork and link the reproduced example?

@airxiaotian
Copy link
Author

https://github.com/airxiaotian/d3-org-bug-reproduce

everything working right if entering /d3 from home page.
but if refresh the /d3 page or type localhost:3000/d3 to the browser, the error occured

@ouin81
Copy link

ouin81 commented Sep 3, 2021

hi, i reproduced this problem.

environment:
MacOS Big Sur 11.5.2

➜   node -v
v14.17.4
➜   yarn -v
1.22.11

create a next-app
https://nextjs.org/

$ yarn create next-app


yarn create v1.22.11
[1/4] 🔍  Resolving packages...
[2/4] 🚚  Fetching packages...
[3/4] 🔗  Linking dependencies...
[4/4] 🔨  Building fresh packages...
success Installed "create-next-app@11.1.2" with binaries:
      - create-next-app
✔ What is your project named? … d3-org-sample
Creating a new Next.js app in path/to/d3-org-sample.

Using yarn.

add d3-org-chart

cd d3-org-sample 
yarn add d3-org-chart
yarn add v1.22.11
[1/4] 🔍  Resolving packages...
.....

add only one line of code into ./pages/index.js

import { OrgChart } from 'd3-org-chart';

start up server, then access http://localhost:3000, the problem reproduced.

yarn dev
yarn run v1.22.11
$ next dev
ready - started server on 0.0.0.0:3000, url: http://localhost:3000
info  - Using webpack 5. Reason: Enabled by default https://nextjs.org/docs/messages/webpack5
event - compiled successfully
event - build page: /
wait  - compiling...
warn  - ./node_modules/d3-flextree/src/flextree.js
Should not import the named export 'version' (imported as 'version') from default-exporting module (only default export is available soon)
info  - ready on http://localhost:3000
event - build page: /next/dist/pages/_error
wait  - compiling...
warn  - ./node_modules/d3-flextree/src/flextree.js
Should not import the named export 'version' (imported as 'version') from default-exporting module (only default export is available soon)
info  - ready on http://localhost:3000
error - path/to/d3-org-sample/node_modules/d3-org-chart/src/d3-org-chart.js:1
import { selection, select } from "d3-selection";
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at wrapSafe (internal/modules/cjs/loader.js:988:16)
    at Module._compile (internal/modules/cjs/loader.js:1036:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)
    at Module.load (internal/modules/cjs/loader.js:937:32)
    at Function.Module._load (internal/modules/cjs/loader.js:778:12)
    at Module.require (internal/modules/cjs/loader.js:961:19)
    at require (internal/modules/cjs/helpers.js:92:18)
    at Object.d3-org-chart (path/to/d3-org-sample/.next/server/pages/index.js:86:18)
    at __webpack_require__ (path/to/d3-org-sample/.next/server/webpack-runtime.js:33:42)
    at eval (webpack-internal:///./pages/index.js:11:70) {
  page: '/'
}

@bumbeishvili
Copy link
Owner

bumbeishvili commented Sep 3, 2021

Ok, that error seems in line with the following issues

#77
#78

At this point, I am not sure yet how can I fix it, will think about it once I will find time from my busy schedule

@ouin81
Copy link

ouin81 commented Sep 3, 2021

@bumbeishvili
hi, the version 2.1.0 is working.

@bumbeishvili
Copy link
Owner

Are you sure? Because nothing changed between 2.1.0 and latest 2.3.4, which would cause one to work and second not

@ouin81
Copy link

ouin81 commented Sep 3, 2021

sorry, i confirmed again.
it was a mistake. #83 (comment)

@airxiaotian
Copy link
Author

can I know your node version?
my node version is 14.17.4

@torchsmith
Copy link

This directly relates to this issue:
asyncapi/asyncapi-react#177 (comment)

Seems to be an issue with server side rendering with NextJS.

I was able to solve the issue by importing the OrgChart class like so:

let OrgChart; import('d3-org-chart').then((mod) => OrgChart = mod.OrgChart);

Opposed to the suggested react implementation:

import { OrgChart } from 'd3-org-chart';

@torchsmith
Copy link

I also switched useLayoutEffect for juts useEffect (since SSR won't be able to use the useLayoutEffect hook effectively)

@bumbeishvili
Copy link
Owner

Good to know, thanks

@bumbeishvili
Copy link
Owner

This issue should be fixed now since #77 is fixed, closing for now. Let me know if issue reappears

@lfpacheco92
Copy link

I'm facing the same issue on NextJs.

However the error stack is as fallows:

import { selection, select } from "d3-selection";
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1026:15)
    at Module._compile (node:internal/modules/cjs/loader:1061:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1149:10)
    at Module.load (node:internal/modules/cjs/loader:975:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:999:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.d3-org-chart (/home/lucas/Dev/userh/bootstrap-frontend/.next/server/pages/management/organization.js:435:18)
    at __webpack_require__ (/home/lucas/Dev/userh/bootstrap-frontend/.next/server/webpack-runtime.js:33:43

@bumbeishvili bumbeishvili reopened this Nov 22, 2021
@Lfaufau
Copy link

Lfaufau commented Jan 19, 2022

I'm facing the same issue on NextJs.

Manually setting "type": "module", in d3-org-chart package.json fixed the issue

@sdoxsee
Copy link

sdoxsee commented Jun 30, 2023

I'm facing the same issue on NextJs.

Manually setting "type": "module", in d3-org-chart package.json fixed the issue

Oddly, to me at least, this works. How do the upvoters implement this for deployment. Surely you don't run a script to add "type": "module" to the package.json of d3-org-chart.

@bumbeishvili can this not be added in general to the package.json as a PR?

{
  "name": "d3-org-chart",
  "version": "2.7.0",
  "module": "module",
  "type": "module", <-- added manually after npm install
  "description": "Highly customizable org chart, created with d3",
  ...
}

@Standin-Alone
Copy link

any updates?

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

Successfully merging a pull request may close this issue.

8 participants