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.

RSC import/export issues with esm external packages

See original GitHub issue

Verify canary release

  • I verified that the issue exists in Next.js canary release

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 21.4.0: Mon Feb 21 20:36:53 PST 2022; root:xnu-8020.101.4~2/RELEASE_ARM64_T8101
Binaries:
  Node: 16.14.0
  npm: 8.3.1
  Yarn: 1.22.17
  pnpm: 6.30.1
Relevant packages:
  next: 12.1.2-canary.0
  react: 18.0.0-rc.3
  react-dom: 18.0.0-rc.3

What browser are you using? (if relevant)

Chrome 99.0.4844.84

How are you deploying your application? (if relevant)

Vercel

Describe the Bug

I upgraded from 12.1.1-canary.18 to 12.1.2-canary.0 and my Next.js app crashed:

CleanShot 2022-03-27 at 13 10 42@2x

After investigating, it seems that this syntax now causes a crash. I also verified that this is the syntax that’s used in the actual unicode-emoji package published to npm (it’s not transpiled).

Expected Behavior

I’d expect the syntax mentioned above to work, just like it did with 12.1.1-canary.18.

To Reproduce

  1. Install the unicode-emoji package (or any other one with this syntax).
  2. Import and call the exported function (in the case of the mentioned package, that would be getEmojisGroupedBy).

I’m not sure if that’s relevant, but the import is located in a client component (I’m using RSC).

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:1
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
huozhicommented, Mar 28, 2022

@willemliufdmg Do you have a repro for that? what kind of files and next config break your case? I can repro it now, checking!

2reactions
willemliufdmgcommented, Mar 28, 2022

~@willemliufdmg Do you have a repro for that? what kind of files and next config break your case?~ I can repro it now, checking!

I could create a minimal project using npm init next-app -- --ts. I’ve added some dependencies:

{
  "name": "test-next-jest",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "lint": "next lint"
  },
  "dependencies": {
    "next": "12.1.1",
    "react": "17.0.2",
    "react-dom": "17.0.2"
  },
  "devDependencies": {
    "@testing-library/jest-dom": "^5.16.3",
    "@testing-library/react": "^12.1.4",
    "@types/node": "17.0.23",
    "@types/react": "17.0.43",
    "@types/react-dom": "17.0.14",
    "eslint": "8.12.0",
    "eslint-config-next": "12.1.1",
    "jest": "^27.5.1",
    "react-svg-loader": "^3.0.3",
    "typescript": "4.6.3"
  }
}

When importing an SVG as a React component using react-svg-loader I get the following error:

Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.

This is the implementation of the component:

import Link from "next/link";
import CameraIcon from "@icons/camera.svg";

function Anchor() {
  return (
    <Link href="https://google.com">
      <a>
        <CameraIcon />
      </a>
    </Link>
  );
}

export { Anchor };

If I replace the icon with something else e.g. a text. The test will work as expected.

The implementation of the test:

import { render } from "@testing-library/react";
import { Anchor } from "../../src/components/Anchor";

describe("Test", () => {
  it("should test correctly", () => {
    const renderResult = render(<Anchor />);
    expect(renderResult.getByRole("link")).toBeTruthy();
  });
});

This is next.config.js:

/** @type {import('next').NextConfig} */
const nextConfig = {
  reactStrictMode: true,
  webpack: (config, options) => {
    // SVG
    config.module.rules.push({
      test: /\.svg$/,
      use: ["react-svg-loader"],
    });
    return config;
  },
};

module.exports = nextConfig;

jest.setup.ts:

import "@testing-library/jest-dom/extend-expect";
import next from "next";

beforeAll(() => {
  // imports the fetch polyfill
  next({});
});

jest.config.js:

const nextJest = require("next/jest");

const createJestConfig = nextJest({
  // Provide the path to your Next.js app to load next.config.js and .env files in your test environment
  dir: "./",
});

// Add any custom config to be passed to Jest
const customJestConfig = {
  testPathIgnorePatterns: ["/node_modules/", ".json"],
  setupFilesAfterEnv: ["<rootDir>/jest.setup.ts"],
  // if using TypeScript with a baseUrl set to the root directory then you need the below for alias' to work
  moduleDirectories: ["node_modules", "<rootDir>/"],
  testEnvironment: "jest-environment-jsdom",
  moduleNameMapper: {
    "^@components/(.*)$": "<rootDir>/src/components/$1",
    "^@icons/(.*)$": "<rootDir>/src/icons/$1",
  },
  collectCoverage: true,
  collectCoverageFrom: ["src/components/**/*"],
  coverageReporters: [
    "clover",
    "json",
    "lcov",
    "text",
    "json-summary",
    "text-summary",
  ],
  resetMocks: true,
  clearMocks: true,
};

// createJestConfig is exported this way to ensure that next/jest can load the Next.js config which is async
module.exports = createJestConfig(customJestConfig);

camera.svg:

<svg width="48" height="48" viewBox="0 0 48 48" fill="none" xmlns="http://www.w3.org/2000/svg">
<circle cx="24" cy="24" r="24" fill="#1F1F1F"/>
<path d="M29 22.5V19C29 18.45 28.55 18 28 18H16C15.45 18 15 18.45 15 19V29C15 29.55 15.45 30 16 30H28C28.55 30 29 29.55 29 29V25.5L33 29.5V18.5L29 22.5Z" fill="white"/>
</svg>
Read more comments on GitHub >

github_iconTop Results From Across the Web

How Importing and Exporting Impacts the Economy
A rising level of imports and a growing trade deficit can have a negative effect on a country's exchange rate. A weaker domestic...
Read more >
Acronyms Abbreviations &Terms - FEMA
Air Transportable RADIAC Package ... 1) Correspondence and Issues Management ... Department of Foreign Affairs and International. Trade.
Read more >
DHS Acronyms, Abbreviations, and Terms (DAAT) List
ATRAP, Air Transportable RADIAC Package. | FEMA | ; ATS, Automated Targeting System. | CBP | PLCY | PRIV | ; ATSA, Aviation...
Read more >
import "flask_sqlalchemy" could not be resolved ... - You.com
This article will give some solutions to fix the NameError: name 'false' is not defined […] Github Issues.
Read more >
xdm - npm
TypeScript icon, indicating that this package has built-in type declarations. 3.4.0 • Public • Published 7 months ago.
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