CJS -> ESM interoperability for imports inside external modules loaded from CDN
See original GitHub issueWhat version of Next.js are you using?
12.0.7
What version of Node.js are you using?
12.22.6 and above
What browser are you using?
Chrome
What operating system are you using?
macOS
How are you deploying your application?
next start
Describe the Bug
If a CDN esm module uses Head
from next/head
breaks next dev
Here is a esm module using next/head
https://jscdn.teleporthq.io/new-project-68d4/globals.js@0888a7f951f31cc0e33ee614e97d5b6e398ed19c
And it is imported inside https://jscdn.teleporthq.io/new-project-68d4/card.js@edb67533c00984de863a24b4433f30bab9eae03e
Now, if we try to import and use the component. It breaks the render.
import styles from '../styles/Home.module.css'
import Component from 'https://jscdn.teleporthq.io/new-project-68d4/card.js@edb67533c00984de863a24b4433f30bab9eae03e'
const App = () => {
return (
<div className={styles.container}>
<Component />
</div>
)
}
export default App
Expected Behavior
Component to load the module and render it without breaking
To Reproduce
Here is a online repl which does the same to reproduce the bug https://replit.com/@JayaKrishnaNamb/nextjs-http#pages/index.js
GitHub repo to reproduce in local https://github.com/teleporthq/nextjs-http
Issue Analytics
- State:
- Created 2 years ago
- Reactions:2
- Comments:8 (3 by maintainers)
Top Results From Across the Web
What the heck are CJS, AMD, UMD, and ESM in Javascript?
The loading and parsing for ESM modules is indeed asynchronous, but the execution of the code in them is synchronous and serialized based...
Read more >Dynamically Import ESM Modules From A CDN - ITNEXT
The URL of the library on the CDN can then be used to dynamically import, await import(cdn) , the functions we are interested...
Read more >CommonJS (cjs) and Modules (esm): Import compatibility
You can import CommonJS (cjs) into ESM modules, and vice versa (esm into cjs). But one way is easier than the other.
Read more >ECMAScript modules in browsers - JakeArchibald.com
"Bare" import specifiers aren't currently supported · nomodule for backwards compatibility · Defer by default · Inline scripts are also deferred.
Read more >ECMAScript modules | Node.js v19.3.0 Documentation
For better compatibility with existing usage in the JS ecosystem, Node.js in ... In most cases, the ES module import can be used...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I believe
styled-components
works because it does something similar to what I mentioned above in Option C, where thedefault
property is interpolated within the package itself:styled-components.cjs.js
styled-components.js
next/dist/shared/lib/head.js (doesn’t interpolate
default
like above)On a separate note it appears that imports have some node module resolution. The reason I say some is because you can’t use
require
within a CDN asset (it’s undefined).And i am not sure if nextjs controls the
module-resolution
of it’s own. Or just delegates to the node one. Because, the same example withstyled-components
is resolving the same irrespective of imported from the code directly or via CDN module. Here is a example https://codesandbox.io/s/new-project1-forked-z64zx?file=/src/teleporthq/pages/home.js