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.

static import of admin modules gives an error

See original GitHub issue

Description

Dynamic import of admin modules works fine, used like here: https://github.com/api-platform/api-platform/blob/62ae3e62685d3b7500049606f7acb9e25dd7fe49/pwa/pages/admin/index.tsx#L9

But static import gives error:

// pages/admin/index.tsx
import Head from "next/head";
import { useEffect, useState } from "react";
import { ListGuesser } from "@api-platform/admin";

const MyList = props =>
  <ListGuesser {...props} hasShow={false} />;

const Admin = () => {
(...)
Uncaught Error: require() of ES Module /srv/app/node_modules/.pnpm/@api-platform+api-doc-parser@0.15.7/node_modules/@api-platform/api-doc-parser/lib/cjs/index.js from /srv/app/node_modules/.pnpm/@api-platform+admin@3.3.5_q3lhx7j73grdkfcigv3e6242um/node_modules/@api-platform/admin/lib/cjs/hydra/dataProvider.js not supported.
index.js is treated as an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which declares all .js files in that package scope as ES modules.
Instead rename index.js to end in .cjs, change the requiring code to use dynamic import() which is available in all CommonJS modules, or change "type": "module" to "type": "commonjs" in /srv/app/node_modules/.pnpm/@api-platform+api-doc-parser@0.15.7/node_modules/@api-platform/api-doc-parser/package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).

As a workaround I can create MyList component inside Admin component using dynamically imported modules, but without additional trics like useRef passing it as a prop to ResourceGuesser force it to render every time.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
alanpoulaincommented, Sep 13, 2022

Maybe it was not a good idea to add type: module in the api-doc-parser 😅

0reactions
alanpoulaincommented, Sep 14, 2022

It was not updated yet 😉 Great to know that it works! All the module changes are hard to follow and it’s very difficult to make a compatible library.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error [ERR_MODULE_NOT_FOUND]: Cannot find module
This answer was the one that helped me! I was using import and the file was .js - Adding --es-module-specifier-resolution=node saved the day...
Read more >
The specified module 'Module Name' was not loaded because ...
Problem: When trying to import the PnP PowerShell module, I got an error message “Import-Module: The specified module 'SharePointPnPPowerShellOnline' was ...
Read more >
Dynamic Import, Code Splitting, Lazy Loading, and Error ...
This article is a detailed guide on how to use dynamic import, which enables code splitting and lazy loading. It also describes how...
Read more >
types/express-serve-static-core 4.17.20 causing build ... - GitHub
I tried using the @types/express-serve-static-core package and had problems. I tried using the latest stable version of tsc.
Read more >
Application Error Loading 'Successfully Deployed' Web App ...
I recently added my first static files for my sjango App. Everything works when I perform py manage.py runserver to test the app...
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