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

Help with barebones Next.js and Paper.js project #2050

Open
thomas901 opened this issue Oct 7, 2023 · 8 comments
Open

Help with barebones Next.js and Paper.js project #2050

thomas901 opened this issue Oct 7, 2023 · 8 comments

Comments

@thomas901
Copy link

Description/Steps to reproduce

Hi I can't seem to get Paper.js working with an empty Next.js project. If anyone could offer some help with a basic working example that would be great.

  1. npm i
  2. npm run dev

In src/app/page.tsx

"use client";
import { useEffect, useRef } from "react";
import { PaperScope } from "paper";

export default function Home() {
  useEffect(() => {
    // throws error on any use of paper lib
    new PaperScope();
  }, []);
  return <canvas></canvas>;
}

Resulting error:

⨯ ./node_modules/paper/dist/node/canvas.js:38:0
Module not found: Can't resolve 'jsdom/lib/jsdom/living/generated/utils'

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

Import trace for requested module:
./node_modules/paper/dist/node/self.js
./node_modules/paper/dist/paper-full.js
./src/app/page.tsx
 ○ Compiling /not-found ...
 ⨯ ./node_modules/paper/dist/node/canvas.js:38:0
Module not found: Can't resolve 'jsdom/lib/jsdom/living/generated/utils'

Link to reproduction test-case

https://github.com/thomas901/minimal-reproduction

Expected result

Should run without any errors.

Additional information

@SixK
Copy link

SixK commented Oct 7, 2023

As you have messages about jsdom, I would try using paper-jsdom or paper-jsdom-canvas packages instead of paper.
But I have no idea if it's the solution.

@rfried
Copy link

rfried commented Nov 1, 2023

I'm running into this same issue with Nuxt 3.8.0. Seems the newest framework versions are expecting something different from importing the paper module.

Here's my reproduction of the issue: https://stackblitz.com/edit/github-dspzws

@alecmolloy
Copy link

just came looking for a solution to the same error.

@SixK i am assuming @thomas901 is not trying to render on node, but is trying to render on the client, as his 'use client' directive means. That said, seems like something is triggering paper to assume it should be rendering on the server, hence the error?

@alecmolloy
Copy link

alecmolloy commented Nov 9, 2023

tried out a fix someone made with gatsby, which also uses webpack, but sadly still didn't work:

/** @type {import('next').NextConfig} */
const nextConfig = {
  webpack: (config, { isServer }) => ({
    ...config,
    module: {
      ...config.module,
      rules: [
        ...config.module.rules,
        ...(isServer
          ? [
              {
                test: /node_modules\/paper/,
                use: 'null-loader',
              },
            ]
          : []),
      ],
    },
  }),
}

module.exports = nextConfig

related: #1483

@btmccord
Copy link

Anyone figure this out? Having the same issue

@JosefDuda
Copy link

What worked for me (Next.js v. 14) was to ensure that Paper.js is ignored in the server code by setting a webpack alias.

next.config.mjs

/** @type {import('next').NextConfig} */

const nextConfig = {
  // ...other settings
  webpack: (config, { isServer }) => {
    if (isServer) {
      config.resolve.alias['paper'] = false;
    }
    return config;
  },
};

export default nextConfig;

@DustinsCode
Copy link

What worked for me (Next.js v. 14) was to ensure that Paper.js is ignored in the server code by setting a webpack alias.

next.config.mjs

/** @type {import('next').NextConfig} */

const nextConfig = {
  // ...other settings
  webpack: (config, { isServer }) => {
    if (isServer) {
      config.resolve.alias['paper'] = false;
    }
    return config;
  },
};

export default nextConfig;

This worked for me, thank you so much 🙌

@eivindml
Copy link

eivindml commented Oct 7, 2024

I tried the config above, but was not able to get it to work. It still not ignored on the server.

Skjermbilde 2024-10-07 kl  15 08 44

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

8 participants