create account

Import vs Require JavaScript Fail by makerhacks

View this thread on: hive.blogpeakd.comecency.com
· @makerhacks ·
$9.79
Import vs Require JavaScript Fail
![fail.png](https://files.peakd.com/file/peakd-hive/makerhacks/AL7RaweDoYg7FtrfeZp9Ghr5GEmzoFj6xHddZU2aqYFBxJV7AZwQDigT3VUGgSv.png)

As you might have already seen, I am MUCH more used to Python, C, etc than modern JavaScript. Things have changed a lot since I last used it for anything but client side.

Problem is, I am doing some projects with a gang of lads who have pivoted from WordPress to using NodeJS, so in the deep end I go!

During development, we ran into repeated issues using certain open-source libraries because of how JavaScript modules are loaded.

There are two main styles of loading code in JavaScript:

- `require()` is the older "CommonJS" method (used in traditional Node.js)
- `import ... from` is the newer "ES Module" method (used in modern projects and browsers)

### What Are ES Modules and How Are They Different

ES Modules (ECMAScript Modules) are the modern standard for structuring JavaScript code. They are designed to be consistent, support better tooling, and match how code is shared on the web.

Key differences:

- Syntax:
  - CommonJS: `const fs = require('fs');`
  - ES Module: `import fs from 'fs';`

- Exporting:
  - CommonJS: `module.exports = myFunction;`
  - ES Module: `export default myFunction;`

- Loading:
  - CommonJS loads modules synchronously (one at a time)
  - ES Modules are loaded asynchronously and follow stricter rules

- Scope:
  - CommonJS runs in a local scope
  - ES Modules run in strict mode by default

### Why This Matters

If your project is set to use ES Modules (by adding `"type": "module"` to `package.json`), you can no longer use `require()`. Many older npm libraries still expect to be loaded with `require()`, and mixing the two formats leads to errors or missing functionality.

### Real Example That Caused Problems

We tried to use this with an ES Module project:

```js
const unified = require('unified'); // Not allowed in ES Module mode
```

So we tried:

```js
import unified from 'unified'; // This threw "unified is not a function"
```

The correct version was:

```js
import { unified } from 'unified'; // Named import works correctly
```

Another example that failed:

```js
const remarkDocx = require('remark-docx'); // Not allowed in ES Module projects
```

This also failed:

```js
import remarkDocx from 'remark-docx'; // remarkDocx is not a function
```

We ended up replacing the library entirely with one that supports modern imports cleanly.

### How You Might Run Into This

You’ll likely hit these issues if you:

- Use `"type": "module"` in your `package.json`
- Try to use `require()` in a modern project
- Use Electron, Vite, Astro, or other modern tools that expect ES Module format
- Mix imports and requires without checking the library’s export format

Common error messages:

- `require is not defined`
- `x is not a function`
- `Cannot set properties of undefined`
- `Cannot find package 'x'` even though it’s installed

### What We Did to Fix It

- Used `await import('package')` instead of `require('package')`
- Accessed `.default` only when needed, and only on known CommonJS packages
- Avoided mixing both `import` and `require` in the same file
- Replaced `remark-docx` with `docx` for exporting Word documents, because it works cleanly with ES Modules

## Bottom Line

Some open-source libraries don’t fully support the modern JavaScript format we use in this app. That caused strange bugs and crashes, especially when exporting to Word with images. We resolved the issues by changing how we load code and switching to a better-supported export library.

This change makes the app more stable and easier to maintain over time but it is a heck of a mind-eff for people who are new to this kind of JS development and just want to go back to the cozy world of basically anything else!
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 393 others
properties (23)
authormakerhacks
permlinkimport-vs-require-javascript-fail
categorydevelopers
json_metadata"{"app":"peakd/2025.4.6","format":"markdown","description":"Some open-source libraries don’t fully support the modern JavaScript","tags":["developers","programming","technology","javascript"],"users":[],"image":["https://files.peakd.com/file/peakd-hive/makerhacks/AL7RaweDoYg7FtrfeZp9Ghr5GEmzoFj6xHddZU2aqYFBxJV7AZwQDigT3VUGgSv.png"]}"
created2025-04-30 18:08:48
last_update2025-04-30 18:08:48
depth0
children1
last_payout2025-05-07 18:08:48
cashout_time1969-12-31 23:59:59
total_payout_value4.925 HBD
curator_payout_value4.867 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length3,800
author_reputation156,478,986,501,043
root_title"Import vs Require JavaScript Fail"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd0
post_id142,421,686
net_rshares29,546,775,293,939
author_curate_reward""
vote details (457)
@stemsocial ·
re-makerhacks-import-vs-require-javascript-fail-20250501t040848971z
<div class='text-justify'> <div class='pull-left'>
 <img src='https://stem.openhive.network/images/stemsocialsupport7.png'> </div>

Thanks for your contribution to the <a href='/trending/hive-196387'>STEMsocial community</a>. Feel free to join us on <a href='https://discord.gg/9c7pKVD'>discord</a> to get to know the rest of us!

Please consider delegating to the @stemsocial account (85% of the curation rewards are returned).

Consider setting @stemsocial as a beneficiary of this post's rewards if you would like to support the community and contribute to its mission of promoting science and education on Hive.&nbsp;<br />&nbsp;<br />
</div>
properties (22)
authorstemsocial
permlinkre-makerhacks-import-vs-require-javascript-fail-20250501t040848971z
categorydevelopers
json_metadata{"app":"STEMsocial"}
created2025-05-01 04:08:48
last_update2025-05-01 04:08:48
depth1
children0
last_payout2025-05-08 04:08:48
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length646
author_reputation22,918,176,844,004
root_title"Import vs Require JavaScript Fail"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id142,429,638
net_rshares0