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

[BUG]: Can't build NextJS app from source #420

Open
oleole39 opened this issue Oct 25, 2024 · 4 comments
Open

[BUG]: Can't build NextJS app from source #420

oleole39 opened this issue Oct 25, 2024 · 4 comments
Assignees
Labels
bug Something isn't working

Comments

@oleole39
Copy link
Contributor

Issue description

Hello,

I have tried to build the following way:

sudo useradd jsoncrack
git clone https://github.com/AykutSarac/jsoncrack.com.git
cd jsoncrack.com/src
sudo corepack enable #has to be enabled at root level
sudo -u jsoncrack corepack enable pnpm
sudo -u jsoncrack corepack use pnpm@latest
sudo -u jsoncrack pnpm install --config.confirmModulesPurge=false #--config option is here to solve issue that COREPACK_ENABLE_DOWNLOAD_PROMPT=0 was not enough to solve.
sudo -u jsoncrack tar -cf parent.tar ../ && tar -xf parent.tar #copy all parent files in /src folder to mimic the build process of the Dockerfile 
chown -R "jsoncrack:www-data" .
rm ../.eslintrc.json #without that step the build is launched by default using this file instead of the one in /src where are located node modules 
sudo -u jsoncrack COREPACK_ENABLE_DOWNLOAD_PROMPT=0 pnpm exec next telemetry disable
sudo -u jsoncrack pnpm build --debug

but it fails with the following error:

./src/containers/Editor/components/views/GraphView/lib/utils/getNodePath.ts
./src/containers/Editor/components/views/GraphView/lib/jsonParser.ts
./src/containers/Editor/components/views/GraphView/stores/useGraph.ts
./pages/widget.tsx

./src/node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/reaflow/dist/index.umd.cjs
Module not found: ESM packages (d3-shape) need to be imported. Use 'import' to reference the package instead. https://nextjs.org/docs/messages/import-esm-externals

https://nextjs.org/docs/messages/module-not-found

Import trace for requested module:
./src/containers/Editor/components/views/GraphView/lib/utils/getNodePath.ts
./src/containers/Editor/components/views/GraphView/lib/jsonParser.ts
./src/containers/Editor/components/views/GraphView/stores/useGraph.ts
./pages/widget.tsx

> Build failed because of webpack errors

I assume the error comes from the require("reaflow") line in /src/containers/Editor/components/views/GraphView/lib/utils/getNodePath.ts

I have tried several things without success, including what is suggested in NextJS docs, that is to say replacing the first lines of getNodePath.ts with something like:

import type { NodeData, EdgeData } from "src/types/graph";
import dynamic from 'next/dynamic';

export function getNodePath(nodes: NodeData[], edges: EdgeData[], nodeId: string) {
  const getParentsForNodeId = dynamic(() => import("reaflow"), { 
    ssr:false 
  });  

But then the build fails with following error:

Type error: Argument of type '() => Promise<(nodes: NodeData<any>[], edges: EdgeData<any>[], startId: string) => any[]>' is not assignable to parameter of type 'DynamicOptions<NodeData<any>[]> | Loader<NodeData<any>[]>'.
  Type '() => Promise<(nodes: NodeData<any>[], edges: EdgeData<any>[], startId: string) => any[]>' is not assignable to type '() => LoaderComponent<NodeData<any>[]>'.
    Type 'Promise<(nodes: NodeData<any>[], edges: EdgeData<any>[], startId: string) => any[]>' is not assignable to type 'LoaderComponent<NodeData<any>[]>'.
      Type '(nodes: NodeData<any>[], edges: EdgeData<any>[], startId: string) => any[]' is not assignable to type 'ComponentType<NodeData<any>[]> | ComponentModule<NodeData<any>[]>'.
        Type '(nodes: NodeData<any>[], edges: EdgeData<any>[], startId: string) => any[]' is not assignable to type 'FunctionComponent<NodeData<any>[]>'.
          Target signature provides too few arguments. Expected 3 or more, but got 2.

> 6 |   const getParentsForNodeId = dynamic(() => import("reaflow"), {
  7 |     ssr: false,
  8 |   });
  9 |

Or if I use instead as found somewhere on the Internet:

const getParentsForNodeId = dynamic(() => import("reaflow").then(c => c.getParentsForNodeId), {
  ssr: false,
});

a similar error:

Type error: Argument of type '() => Promise<(nodes: NodeData<any>[], edges: EdgeData<any>[], startId: string) => any[]>' is not assignable to parameter of type 'DynamicOptions<NodeData<any>[]> | Loader<NodeData<any>[]>'.
  Type '() => Promise<(nodes: NodeData<any>[], edges: EdgeData<any>[], startId: string) => any[]>' is not assignable to type '() => LoaderComponent<NodeData<any>[]>'.
    Type 'Promise<(nodes: NodeData<any>[], edges: EdgeData<any>[], startId: string) => any[]>' is not assignable to type 'LoaderComponent<NodeData<any>[]>'.
      Type '(nodes: NodeData<any>[], edges: EdgeData<any>[], startId: string) => any[]' is not assignable to type 'ComponentType<NodeData<any>[]> | ComponentModule<NodeData<any>[]>'.
        Type '(nodes: NodeData<any>[], edges: EdgeData<any>[], startId: string) => any[]' is not assignable to type 'FunctionComponent<NodeData<any>[]>'.
          Target signature provides too few arguments. Expected 3 or more, but got 2.

  4 | export function getNodePath(nodes: NodeData[], edges: EdgeData[], nodeId: string) {
  5 |   // eslint-disable-next-line @typescript-eslint/no-var-requires
> 6 |   const getParentsForNodeId = dynamic(() => import("reaflow").then(c => c.getParentsForNodeId), {
    |                                       ^
  7 |     ssr: false,
  8 |   });
  9 |

Would you have any clue ?

Media & Screenshots

No response

Operating system

  • OS: Debian 12
  • Nodejs: 20.18.0
  • pnpm: latest (9.12.2)
  • Next.js: 14.2.3

Priority this issue should have

Low (slightly annoying)

@oleole39 oleole39 added the bug Something isn't working label Oct 25, 2024
@AykutSarac
Copy link
Owner

Can you try again using Docker?

@oleole39
Copy link
Contributor Author

oleole39 commented Oct 31, 2024

Can you try again using Docker?

You mean just to check ?
Because in the end I am looking for a way to build it without Docker (I'd like to package it for Yunohost).

@oleole39
Copy link
Contributor Author

oleole39 commented Nov 21, 2024

@AykutSarac Not sure whether it is rather you updating the build process or me trying to build it in a different way, but I now manage to build it, although there are warnings and path issues.

Here are my new build steps on a fresh ubuntu-latest Github Actions' VM:

    corepack enable pnpm && pnpm install --no-frozen-lockfile
    COREPACK_ENABLE_DOWNLOAD_PROMPT=0 pnpm exec next telemetry disable
    pnpm run lint:fix
    pnpm run build

A few notes:

  • If I use pnpm install --frozen-lockfile I get an error and node modules are not downloaded. Using --no-frozen-lock-files flag enables to build, but it might bring other issues... Would pnpm version used to generate the pnpm-lock.yaml not be version 9.1.4 as defined in package.json ?

    pnpm install frozen-lockfile error
      + corepack enable pnpm
      + pnpm install --frozen-lockfile
      ! Corepack is about to download https://registry.npmjs.org/pnpm/-/pnpm-9.1.4.tgz
       ERR_PNPM_OUTDATED_LOCKFILE  Cannot install with "frozen-lockfile" because pnpm-lock.yaml is not up to date with package.json
    
      Note that in CI environments this setting is true by default. If you still need to run install in such cases, use "pnpm install --no-frozen-lockfile"
    
      	Failure reason:
      	specifiers in the lockfile ({"@mantine/code-highlight":"^7.11.2","@mantine/core":"^7.11.2","@mantine/dropzone":"^7.11.2","@mantine/hooks":"^7.11.2","@monaco-editor/react":"^4.6.0","@sentry/nextjs":"^7.118.0","@tanstack/react-query":"^4.36.1","allotment":"^1.20.2","axios":"^1.7.2","dayjs":"^1.11.12","gofmt.js":"^0.0.2","html-to-image":"^1.11.11","jq-web":"^0.5.1","js-cookie":"^3.0.5","js-yaml":"^4.1.0","json-2-csv":"^5.5.4","json-schema-faker":"^0.5.6","json_typegen_wasm":"^0.7.0","jsonc-parser":"^3.3.1","jsonpath-plus":"^10.0.0","jsonwebtoken":"^9.0.2","jxon":"2.0.0-beta.5","lodash.debounce":"^4.0.8","next":"14.2.10","next-seo":"^6.5.0","next-sitemap":"^4.2.3","nextjs-google-analytics":"^2.3.7","react":"^18.3.1","react-compare-slider":"^3.1.0","react-countup":"^6.5.3","react-dom":"^18.3.1","react-hot-toast":"^2.4.1","react-icons":"^5.2.1","react-json-tree":"^0.18.0","react-linkify-it":"^1.0.8","react-text-transition":"^3.1.0","react-zoomable-ui":"^0.11.0","reaflow":"5.2.8","styled-components":"^6.1.12","toml":"^3.0.0","use-long-press":"^3.2.0","zustand":"^4.5.4","@next/bundle-analyzer":"^14.2.5","@trivago/prettier-plugin-sort-imports":"^4.3.0","@types/js-cookie":"^3.0.6","@types/js-yaml":"^4.0.9","@types/jsonwebtoken":"^9.0.6","@types/jxon":"^2.0.5","@types/node":"^20.14.11","@types/react":"18.2.51","@types/react-dom":"^18.3.0","@typescript-eslint/eslint-plugin":"^7.17.0","eslint":"8.56.0","eslint-config-next":"14.1.0","eslint-config-prettier":"^9.1.0","eslint-plugin-prettier":"^5.2.1","eslint-plugin-unused-imports":"^3.2.0","prettier":"^3.3.3","ts-node":"^10.9.2","typescript":"5.3.3"}) don't match specs in package.json ({"@next/bundle-analyzer":"^14.2.5","@trivago/prettier-plugin-sort-imports":"^4.3.0","@types/js-cookie":"^3.0.6","@types/js-yaml":"^4.0.9","@types/jsonwebtoken":"^9.0.6","@types/jxon":"^2.0.5","@types/node":"^20.14.11","@types/react":"18.2.51","@types/react-dom":"^18.3.0","@typescript-eslint/eslint-plugin":"^7.17.0","eslint":"8.56.0","eslint-config-next":"14.1.0","eslint-config-prettier":"^9.1.0","eslint-plugin-prettier":"^5.2.1","eslint-plugin-unused-imports":"^3.2.0","prettier":"^3.3.3","ts-node":"^10.9.2","typescript":"5.3.3","@mantine/code-highlight":"^7.11.2","@mantine/core":"^7.11.2","@mantine/dropzone":"^7.11.2","@mantine/hooks":"^7.11.2","@monaco-editor/react":"^4.6.0","@sentry/nextjs":"^7.118.0","@tanstack/react-query":"^4.36.1","allotment":"^1.20.2","axios":"^1.7.2","dayjs":"^1.11.12","gofmt.js":"^0.0.2","html-to-image":"^1.11.11","jq-web":"^0.5.1","js-cookie":"^3.0.5","js-yaml":"^4.1.0","json-2-csv":"^5.5.4","json-schema-faker":"^0.5.6","json_typegen_wasm":"^0.7.0","jsonc-parser":"^3.3.1","jsonpath-plus":"^10.0.0","jsonwebtoken":"^9.0.2","jxon":"2.0.0-beta.5","lodash.debounce":"^4.0.8","next":"14.2.10","next-seo":"^6.5.0","next-sitemap":"^4.2.3","react":"^18.3.1","react-compare-slider":"^3.1.0","react-countup":"^6.5.3","react-dom":"^18.3.1","react-hot-toast":"^2.4.1","react-icons":"^5.2.1","react-json-tree":"^0.18.0","react-linkify-it":"^1.0.8","react-text-transition":"^3.1.0","react-zoomable-ui":"^0.11.0","reaflow":"5.2.8","styled-components":"^6.1.12","toml":"^3.0.0","use-long-press":"^3.2.0","zustand":"^4.5.4"})
    
  • I use pnpm lint:fix since before building, I modify a few points which make sense only on jsoncrack.com website. These are rather light changes which I don't expect to break the app. Without lint:fix, pnpm build complains about formatting issue such as empty lines due to the modifications.

So with these steps, the build is successful, but there are following warnings:

Warnings in build log
> next build

⚠ No build cache found. Please configure build caching for faster rebuilds. Read more: https://nextjs.org/docs/messages/no-cache
Attention: Next.js now collects completely anonymous telemetry regarding usage.
This information is used to shape Next.js' roadmap and prioritize features.
You can learn more, including how to opt-out if you'd not like to participate in this anonymous program, by visiting the following URL:
https://nextjs.org/telemetry

  ▲ Next.js 14.2.10
  - Environments: .env

   Linting and checking validity of types ...
   Creating an optimized production build ...
error - No Sentry auth token configured. Source maps will not be uploaded.
You can find information on how to generate a Sentry auth token here: https://docs.sentry.io/api/auth/
After generating a Sentry auth token, set it via the SENTRY_AUTH_TOKEN environment variable during the build.

 ⚠ Compiled with warnings

./node_modules/.pnpm/[email protected]/node_modules/json_typegen_wasm/json_typegen_wasm_bg.wasm
The generated code contains 'async/await' because this module is using "asyncWebAssembly".
However, your target environment does not appear to support 'async/await'.
As a result, the code may not run as expected or may cause runtime errors.

Import trace for requested module:
./node_modules/.pnpm/[email protected]/node_modules/json_typegen_wasm/json_typegen_wasm_bg.wasm
./node_modules/.pnpm/[email protected]/node_modules/json_typegen_wasm/json_typegen_wasm.js
./src/lib/utils/generateType.ts

I'm not too worried about Sentry-related warnings, but what about the json_typegen_wasm-related?

Eventually, resources' paths are all absolute paths - e.g. href="/_next/static/css/92034e62b7b334cd.css" - thus breaking the app, which is not likely to be hosted at root level.
I can grep -rli '"/_next' ./* | xargs sed -i 's@"/_next@"./_next@g' in the app folder but it is not exactly perfect (probably there are other patterns). Would there be a way to change app's base path from root (/) to something else at build (e.g. /MY_PATH) or make them relative ?

@oleole39
Copy link
Contributor Author

oleole39 commented Nov 28, 2024

Would there be a way to change app's base path from root (/) to something else at build (e.g. /MY_PATH) or make them relative ?

I was eventually able to manage to customize base root by adding
basePath: "/my_custom_path", to file next.config.js just before current line 10.

Additionally I had to change the following files to avoid broken/unconsistent links:

sed -i 's/\/editor/\/my_custom_path\/editor"/g' "./public/manifest.json"
sed -i 's/assets/\/my_custom_path\/assets"/g' "./public/manifest.json"
sed -i 's/jsoncrack.com/my_custom_domain\/my_custom_path/g' "./src/constants/seo.ts"
sed -i 's/"\/manifest.json"/"\/my_custom_path\/manifest.json"/g' "./src/constants/seo.ts"
sed -i 's/"\/favicon.ico"/"\/my_custom_path\/favicon.ico"/g' "./src/constants/seo.ts"
sed -i 's/"\/assets/"\/my_custom_path\/assets/g' "./src/containers/Editor/components/LiveEditor.tsx"
sed -i 's/"\/assets/"\/my_custom_path\/assets/g' "./src/containers/Editor/components/views/GraphView/index.tsx"
sed -i 's/"\/assets/"\/my_custom_path\/assets/g' "./src/layout/JsonCrackLogo.tsx"

Other questions in previous post are still unanswered - I can't use --frozen-lockfile and wonder about json_typegen_wasm-related warnings, but overall I finally managed to get a working build.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants