UMD file does not work in nextjs.
See original GitHub issueHi Team,
I am trying to build a npm module using microbundle. I have to make it work for both react js (also other front end frameworks) apps and next js apps. However, I am not able to achieve it. Below is the configuration that I am using -
"scripts": {
"build": "microbundle --sourcemap false && npm run microbundle-standalone",
"microbundle-standalone": "microbundle -o dist/index.min.js --external none --format umd --sourcemap false",
"dev": "microbundle watch",
"test": "echo \"Error: no test specified\" && exit 1"
},
This script generates a “umd” file which is what I am eventually using for npm module in the reactjs based application. The same file doesn’t work for nextjs. Issue that I face using using the npm package in nextjs -
Error: Cannot find module '<path-to-node-modules>/dist/index.modern.js'
at createEsmNotFoundErr (internal/modules/cjs/loader.js:842:15)
at finalizeEsmResolution (internal/modules/cjs/loader.js:835:15)
at resolveExports (internal/modules/cjs/loader.js:424:14)
at Function.Module._findPath (internal/modules/cjs/loader.js:464:31)
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:802:27)
at Function.Module._load (internal/modules/cjs/loader.js:667:27)
When I add the same modern js file in the npm module, it stops working for react js projects.
Is there a generic guidance to make a npm module work for both next js and other standard front end frameworks?
Issue Analytics
- State:
- Created 2 years ago
- Comments:7
Top Results From Across the Web
Importing umd.js to Next.js project not working - Stack Overflow
I import the library like so: import { Animation } from '@lottiefiles/lottie-js';. I would appreciate any help with importing this library into ...
Read more >Developers - UMD file does not work in nextjs. - - Bountysource
Hi Team,. I am trying to build a npm module using microbundle. I have to make it work for both react js (also...
Read more >module-not-found - Next.js
The module you're trying to import is in a different directory. Make sure that the path you're importing refers to the right directory...
Read more >How to Bypass ES Modules Errors in Next.js with Dynamic ...
In this article, I'll walk you through an error you may get when you are building JavaScript apps with Next.js, and how to...
Read more >React refers to UMD global, but the current file is a module
First, try restarting your IDE and development server. This solves the issue sometimes. · If the error is not resolved, make sure the...
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 Free
Top 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
Well, from build tool to build tool, how package exports are consumed will change. There’s a good half-dozen popular tools that use React, all of which can (and do!) consume exports differently. This isn’t a client-side React vs server-side React issue. It has nothing to do with React at all, actually. Just the build tool in question.
Well that’d be an issue. Why do you have
exportsset to that path if you’re not uploading that file?Is this coming from CRA? The issue is that
/dist/index.modern.jsis ESM, but tools will expect it to be CJS by default as you have not set"type": "module"or the file extension to.mjs. Without this, CJS is assumed. So CRA tries to load it as if it was CJS, but finds an import statement, which, like the message says, cannot be used outside of a module.Why UMD? Neither of these tools at the moment are touching the UMD, if you didn’t realize this. Nor should UMD really be the go-to these days.
Include your whole
/distfolder, and then renameindex.modern.js->index.modern.mjs. Make sure to update the file path in your pkg.json too..mjsspecifies that this is a module, so it shouldn’t be loaded as if it was CJSHopefully this is answered. Let me know if it should be reopened.