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

Does this work in Next js? #51

Open
3SCadmin opened this issue Dec 27, 2023 · 16 comments
Open

Does this work in Next js? #51

3SCadmin opened this issue Dec 27, 2023 · 16 comments

Comments

@3SCadmin
Copy link

I can get this working in node, but when I try to convert it to a Next js 14 server action, I get this error:

⨯ node_modules\pdf-img-convert\node_modules\pdfjs-dist\legacy\build\pdf.js (5903:35) @ eval
⨯ Error: Setting up fake worker failed: "Cannot find module './pdf.worker.js'

@lahammam
Copy link

I have the same issue in NestJs
npm WARN deprecated [email protected]: dommatrix is no longer maintained. Please use @thednp/dommatrix. npm ERR! code 1 npm ERR! path /Users/geust/Desktop/lendstack-api/node_modules/canvas npm ERR! command failed npm ERR! command sh -c node-pre-gyp install --fallback-to-build --update-binary npm ERR! Failed to execute '/usr/local/bin/node /usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js configure --fallback-to-build --update-binary --module=/Users/geust/Desktop/lendstack-api/node_modules/canvas/build/Release/canvas.node --module_name=canvas --module_path=/Users/geust/Desktop/lendstack-api/node_modules/canvas/build/Release --napi_version=9 --node_abi_napi=napi --napi_build_version=0 --node_napi_label=node-v108' (1) npm ERR! node-pre-gyp info it worked if it ends with ok npm ERR! node-pre-gyp info using [email protected] npm ERR! node-pre-gyp info using [email protected] | darwin | arm64 npm ERR! node-pre-gyp http GET https://github.com/Automattic/node-canvas/releases/download/v2.11.2/canvas-v2.11.2-node-v108-darwin-unknown-arm64.tar.gz npm ERR! node-pre-gyp ERR! install response status 404 Not Found on https://github.com/Automattic/node-canvas/releases/download/v2.11.2/canvas-v2.11.2-node-v108-darwin-unknown-arm64.tar.gz npm ERR! node-pre-gyp WARN Pre-built binaries not installable for [email protected] and [email protected] (node-v108 ABI, unknown) (falling back to source compile with node-gyp) npm ERR! node-pre-gyp WARN Hit error response status 404 Not Found on https://github.com/Automattic/node-canvas/releases/download/v2.11.2/canvas-v2.11.2-node-v108-darwin-unknown-arm64.tar.gz npm ERR! gyp info it worked if it ends with ok npm ERR! gyp info using [email protected] npm ERR! gyp info using [email protected] | darwin | arm64 npm ERR! gyp info ok npm ERR! gyp info it worked if it ends with ok npm ERR! gyp info using [email protected] npm ERR! gyp info using [email protected] | darwin | arm64 npm ERR! gyp info find Python using Python version 3.9.6 found at "/Applications/Xcode.app/Contents/Developer/usr/bin/python3" npm ERR! gyp info spawn /Applications/Xcode.app/Contents/Developer/usr/bin/python3 npm ERR! gyp info spawn args [ npm ERR! gyp info spawn args '/usr/local/lib/node_modules/npm/node_modules/node-gyp/gyp/gyp_main.py', npm ERR! gyp info spawn args 'binding.gyp', npm ERR! gyp info spawn args '-f', npm ERR! gyp info spawn args 'make', npm ERR! gyp info spawn args '-I', npm ERR! gyp info spawn args '/Users/geust/Desktop/lendstack-api/node_modules/canvas/build/config.gypi', npm ERR! gyp info spawn args '-I', npm ERR! gyp info spawn args '/usr/local/lib/node_modules/npm/node_modules/node-gyp/addon.gypi', npm ERR! gyp info spawn args '-I', npm ERR! gyp info spawn args '/Users/geust/Library/Caches/node-gyp/18.18.0/include/node/common.gypi', npm ERR! gyp info spawn args '-Dlibrary=shared_library', npm ERR! gyp info spawn args '-Dvisibility=default', npm ERR! gyp info spawn args '-Dnode_root_dir=/Users/geust/Library/Caches/node-gyp/18.18.0', npm ERR! gyp info spawn args '-Dnode_gyp_dir=/usr/local/lib/node_modules/npm/node_modules/node-gyp', npm ERR! gyp info spawn args '-Dnode_lib_file=/Users/geust/Library/Caches/node-gyp/18.18.0/<(target_arch)/node.lib', npm ERR! gyp info spawn args '-Dmodule_root_dir=/Users/geust/Desktop/lendstack-api/node_modules/canvas', npm ERR! gyp info spawn args '-Dnode_engine=v8', npm ERR! gyp info spawn args '--depth=.', npm ERR! gyp info spawn args '--no-parallel', npm ERR! gyp info spawn args '--generator-output', npm ERR! gyp info spawn args 'build', npm ERR! gyp info spawn args '-Goutput_dir=.' npm ERR! gyp info spawn args ] npm ERR! /bin/sh: pkg-config: command not found npm ERR! gyp: Call to 'pkg-config pixman-1 --libs' returned exit status 127 while in binding.gyp. while trying to load binding.gyp npm ERR! gyp ERR! configure error npm ERR! gyp ERR! stack Error:gyp` failed with exit code: 1
npm ERR! gyp ERR! stack at ChildProcess.onCpExit (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/configure.js:325:16)
npm ERR! gyp ERR! stack at ChildProcess.emit (node:events:517:28)
npm ERR! gyp ERR! stack at ChildProcess._handle.onexit (node:internal/child_process:292:12)
npm ERR! gyp ERR! System Darwin 23.0.0
npm ERR! gyp ERR! command "/usr/local/bin/node" "/usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "configure" "--fallback-to-build" "--update-binary" "--module=/Users/geust/Desktop/lendstack-api/node_modules/canvas/build/Release/canvas.node" "--module_name=canvas" "--module_path=/Users/geust/Desktop/lendstack-api/node_modules/canvas/build/Release" "--napi_version=9" "--node_abi_napi=napi" "--napi_build_version=0" "--node_napi_label=node-v108"
npm ERR! gyp ERR! cwd /Users/geust/Desktop/lendstack-api/node_modules/canvas
npm ERR! gyp ERR! node -v v18.18.0
npm ERR! gyp ERR! node-gyp -v v9.4.0
npm ERR! gyp ERR! not ok
npm ERR! node-pre-gyp ERR! build error
npm ERR! node-pre-gyp ERR! stack Error: Failed to execute '/usr/local/bin/node /usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js configure --fallback-to-build --update-binary --module=/Users/geust/Desktop/lendstack-api/node_modules/canvas/build/Release/canvas.node --module_name=canvas --module_path=/Users/geust/Desktop/lendstack-api/node_modules/canvas/build/Release --napi_version=9 --node_abi_napi=napi --napi_build_version=0 --node_napi_label=node-v108' (1)
npm ERR! node-pre-gyp ERR! stack at ChildProcess. (/Users/geust/Desktop/lendstack-api/node_modules/@mapbox/node-pre-gyp/lib/util/compile.js:89:23)
npm ERR! node-pre-gyp ERR! stack at ChildProcess.emit (node:events:517:28)
npm ERR! node-pre-gyp ERR! stack at maybeClose (node:internal/child_process:1098:16)
npm ERR! node-pre-gyp ERR! stack at ChildProcess._handle.onexit (node:internal/child_process:303:5)
npm ERR! node-pre-gyp ERR! System Darwin 23.0.0
npm ERR! node-pre-gyp ERR! command "/usr/local/bin/node" "/Users/geust/Desktop/lendstack-api/node_modules/.bin/node-pre-gyp" "install" "--fallback-to-build" "--update-binary"
npm ERR! node-pre-gyp ERR! cwd /Users/geust/Desktop/lendstack-api/node_modules/canvas
npm ERR! node-pre-gyp ERR! node -v v18.18.0
npm ERR! node-pre-gyp ERR! node-pre-gyp -v v1.0.11
npm ERR! node-pre-gyp ERR! not ok

npm ERR! A complete log of this run can be found in: /Users/geust/.npm/_logs/2024-03-19T00_55_01_156Z-debug-0.log
`

@jakate
Copy link

jakate commented Apr 25, 2024

I can get this working in node, but when I try to convert it to a Next js 14 server action, I get this error:

⨯ node_modules\pdf-img-convert\node_modules\pdfjs-dist\legacy\build\pdf.js (5903:35) @ eval ⨯ Error: Setting up fake worker failed: "Cannot find module './pdf.worker.js'

For the original question, I think you can make it work by adding the dependecy on next.config

/** @type {import('next').NextConfig} */
const nextConfig = {
  experimental: {
    serverComponentsExternalPackages: ['pdf-img-convert'],
  },
};

export default nextConfig;

@jakate
Copy link

jakate commented May 6, 2024

I can get this working in node, but when I try to convert it to a Next js 14 server action, I get this error:
⨯ node_modules\pdf-img-convert\node_modules\pdfjs-dist\legacy\build\pdf.js (5903:35) @ eval ⨯ Error: Setting up fake worker failed: "Cannot find module './pdf.worker.js'

For the original question, I think you can make it work by adding the dependecy on next.config

/** @type {import('next').NextConfig} */
const nextConfig = {
  experimental: {
    serverComponentsExternalPackages: ['pdf-img-convert'],
  },
};

export default nextConfig;

Okay, seems like this does not work anymore

@AryashDubey
Copy link

Were you able to solve it?

@jakate
Copy link

jakate commented May 13, 2024

I got it working.

This was the next.config.mjs changes I did:

const nextConfig = {
  experimental: {
    serverComponentsExternalPackages: [
      'pdf-img-convert',
    ],
    outputFileTracingIncludes: {
      '/': [
        './node_modules/pdf-img-convert/node_modules/pdfjs-dist/legacy/build/pdf.worker.js',
        './node_modules/pdf-img-convert/node_modules/pdfjs-dist/legacy/build/pdf.js',
      ],
    },
  },
}

And using node version 21

@AryashDubey
Copy link

Thank you so much man! That worked for me :)

@murtazabaanihali
Copy link

This worked for me.

By changing next.config.js or next.config.mjs

const nextConfig = { experimental: { serverComponentsExternalPackages: [ 'pdfjs-dist', 'pdf-img-convert' ], outputFileTracingIncludes: { '/': [ './node_modules/pdfjs-dist/legacy/build/pdf.worker.js', './node_modules/pdfjs-dist/legacy/build/pdf.js', ], }, } }

@ysqander
Copy link

Thanks @jakate , I spent a few hours on this!!

@Trevor-M-Williams
Copy link

I got it working.

This was the next.config.mjs changes I did:

const nextConfig = {
  experimental: {
    serverComponentsExternalPackages: [
      'pdf-img-convert',
    ],
    outputFileTracingIncludes: {
      '/': [
        './node_modules/pdf-img-convert/node_modules/pdfjs-dist/legacy/build/pdf.worker.js',
        './node_modules/pdf-img-convert/node_modules/pdfjs-dist/legacy/build/pdf.js',
      ],
    },
  },
}

And using node version 21

This works in development but is failing for me in prod.

Error: Setting up fake worker failed: "Cannot find module './pdf.worker.js'
Require stack:
- /var/task/node_modules/.pnpm/[email protected]/node_modules/pdfjs-dist/legacy/build/pdf.js
- /var/task/node_modules/.pnpm/[email protected][email protected]/node_modules/pdf-img-convert/pdf-img-convert.js
- /var/task/.next/server/app/dashboard/files/[fileSlug]/page.js
- /var/task/node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/next/dist/compiled/next-server/server.runtime.prod.js
- /var/task/___next_launcher.cjs
- /opt/rust/nodejs.js".
    at /var/task/node_modules/.pnpm/[email protected]/node_modules/pdfjs-dist/legacy/build/pdf.js:5903:36
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

Would the file path be different after building?

@jakate
Copy link

jakate commented Jul 26, 2024

I'm running my app on Vercel and it seems to work with that config just fine.

@Trevor-M-Williams
Copy link

@jakate Is your repo public? I'd love to take a look

@jakate
Copy link

jakate commented Jul 30, 2024

I'm sorry, it's not public, so I can't really share that 😕

@alexbacanu
Copy link

I also encountered some issues with this, but since I was using pnpm in my setup, I had to make a few changes to get it working on Vercel. Here's my configuration:

experimental: {
  serverComponentsExternalPackages: ['pdf-img-convert'],
  outputFileTracingIncludes: {
    '/api/convert-to-img': [
      './node_modules/.pnpm/pdfjs-dist*/node_modules/pdfjs-dist/legacy/build/pdf.worker.js',
      './node_modules/.pnpm/pdfjs-dist*/node_modules/pdfjs-dist/legacy/build/pdf.js',
    ],
  },
},

Note the * after pdfjs-dist because the path looks something like this: /var/task/node_modules/.pnpm/[email protected]/node_modules/pdfjs-dist/legacy/build/pdf.js.

@SebastienPingal
Copy link

@jakate @alexbacanu you saved me there, thanks

@BlakeBrown
Copy link

Wasn't able to get this working, error was

import fetch from 'node-fetch';
^^^^^^

SyntaxError: Cannot use import statement outside a module

@martsie
Copy link

martsie commented Dec 6, 2024

Wasn't able to get this working, error was

import fetch from 'node-fetch';
^^^^^^

SyntaxError: Cannot use import statement outside a module

To solve that, I added transpilePackages: ["pdf-img-convert"], to the next config file:

e.g.

// next.config.ts
const nextConfig: NextConfig = {
  transpilePackages: ["pdf-img-convert"],
};

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

No branches or pull requests