The error Error [ERR_REQUIRE_ESM]: require() of ES Module ... not supported occurs because the package in question has been written as an ES Module (ESM) and cannot be used with require() statements, which are used in CommonJS modules. In this case, you are trying to load the ES Module unocss/dist/vite.mjs using require().
To fix this issue, you have a few approaches depending on the situation:
1. Use import Instead of require
Since unocss provides its module in ESM format, you should use import instead. Update your code to use import instead of require():
Before:
javascript
const unocss = require('unocss/vite.mjs');
After:
javascript
import unocss from 'unocss/vite.mjs';
Note: You must ensure that your Node.js environment is properly set up to support ES modules (type: "module" in package.json) if you’re not using a bundler like Webpack or Vite.
2. Enable ESM in Your Project
If you are using Node.js, you should enable ES Modules in your project by adding "type": "module" in your package.json. This tells Node.js to treat .js files as ESM:
package.json:
json
{
"type": "module"
}
After making this change, you can directly use import statements in your codebase.
3. Use Dynamic import()
If you cannot switch your project to ESM, you can use the import() function, which is available in CommonJS modules, to dynamically load the ES Module:
Example Using import() Dynamically:
javascript
(async () => {
const unocss = await import('unocss/vite.mjs');
// Use the `unocss` module here
})();
This approach allows you to work in a CommonJS module while still loading the ES Module unocss/vite.mjs.
4. Check for CommonJS Compatibility
Some packages offer CommonJS builds specifically for environments that only support require(). Look through the unocss documentation or check its package.json file to see if it lists a CommonJS (.cjs) version of the module. You can then switch to using the CommonJS version.
For instance, if there’s a .cjs version:
javascript
const unocss = require('unocss/cjs/vite.cjs');
Note: In many modern libraries, like unocss, they have completely migrated to ESM, so this may not be an option.
5. Use a Bundler (Vite, Webpack, etc.) if Needed
If the library you are working with (e.g., unocss) requires ESM and your current setup doesn't support it, consider using a bundler like Vite, Webpack, or Rollup. These tools will handle this ESM/CommonJS compatibility for you during the build process.
6. Revert to a Lower Version of the Package
If your current runtime or build system doesn’t support ESM and upgrading is not feasible, consider downgrading the unocss package to an earlier version that still uses CommonJS. For example:
bash
npm install unocss@<compatible-version>
Check the unocss changelogs or repository for details on when they transitioned to ESM (most modern libraries have adopted ESM as the default in recent versions).
Conclusion
The best solution depends on your project setup and compatibility requirements:
If possible, migrate your project to ESM (type: "module") and use import.
For legacy projects, use dynamic import() in CommonJS or find a suitable CommonJS-compatible version of the package.
Let me know if you have questions about specific steps!