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.

ES Modules can't be imported without a bundler

See original GitHub issue

Hello! 👋

Loved seeing the ES Modules support land yesterday 😊

Description of the issue

I’m unfortunately seeing an Error [ERR_PACKAGE_PATH_NOT_EXPORTED] thrown without a bundler

Steps to reproduce the issue

This happens with a plain native Node.js import from another *.mjs file

Current (see package.json) Missing the main or default export

"exports": {
  "import": "./govuk-esm/all.mjs"
}

Fixed (see exports documentation) Provided a main or default export for both import and require()

"exports": {
  ".": {
    "import": "./govuk-esm/all.mjs",
    "require": "./govuk/all.js"
  }
}

Actual vs expected behaviour

Node’s resolver node:internal/modules/esm/resolve doesn’t have enough info to import 'govuk-frontend'

Unlike the Import JavaScript using a bundler guidance regarding webpack, I can’t teach Node.js to break the ESM rules.

With the fix above, I sadly hit another challenge. Node.js wouldn’t attempt to resolve .mjs files as there’s no package.json "type": "module" and extensions are mandatory for CommonJS and ESM compatibility 😬

Mandatory file extensions A file extension must be provided when using the import keyword. Directory indexes (e.g. './startup/index.js') must also be fully specified.

This behavior matches how import behaves in browser environments, assuming a typically configured server.

I wasn’t fully out of options though.

I tried the new ESM aware TypeScript compiler but it also had issues importing without extensions. Microsoft have added some good release notes on type in package.json and New Extensions plus challenges involved.

Hope these notes help, it’s fab to see what’s coming 🚀

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
colinrotherhamcommented, May 31, 2022

@vanitabarrett Here’s a super minimal one: https://github.com/colinrotherham/govuk-frontend-esm-sample

npm ci
npm start

Or via mocha

npm test
1reaction
vanitabarrettcommented, May 31, 2022

Ah interesting, thanks! Will take a look @colinrotherham

Read more comments on GitHub >

github_iconTop Results From Across the Web

Natively import ES module dependencies from npm without ...
I've gotten webpack to split dependencies into a separate file, but to import from that file in a buildless context, I'd have to...
Read more >
The JavaScript Modules Handbook – Complete Guide to ES ...
This article will show you all you need to know about ES Modules and Module Bundlers in plain English. Table of Contents 1....
Read more >
Node Modules at War: Why CommonJS and ES ... - Code Red
ESM scripts can import CJS scripts, but only by using the “default import” syntax import _ from 'lodash' , not the “named import”...
Read more >
Using Native, Unbundled ES6 Modules With a Simple Build ...
The modern import cannot possibly do that, because the module names must be static — the resolution cannot depend on any code being...
Read more >
rollup.js
Importing CommonJS · Publishing ES Modules ... Imported values cannot be reassigned, though imported objects and arrays can be mutated (and the exporting ......
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