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.

While I was testing to add an export map I found the following problem.

e.g. if we add this to minisearch

  "exports": {
    ".": {
      "types": "./dist/types/index.d.ts",
      "require": "./dist/umd/index.js",
      "default": "./dist/es/index.js"
    }
  },

and then try to use it in node

// package.json
  "type": "module"
// test.js
import MiniSearch from 'minisearch';

// execute
node test.js

we get the following error

node test.js
(node:65561) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
/www/testmini/node_modules/minisearch/dist/es/index.js:1436
export default MiniSearch;
^^^^^^

SyntaxError: Unexpected token 'export'

which is correct as we try to import the file dist/es/index.js but the package minisearch is define as a require package… e.g. it has an implicit "type": "commonjs",. e.g. all *.js files are treated as common js

Solutions

  1. add type: "module" to package.json to minisearch so that *.js files are treated as modules (and export the commonjs version as *.cjs files)
  2. export the module version as *.mjs (which is kinda strange for a browser but still works)

What do you think?

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:1
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
daKmoRcommented, Apr 21, 2022

you can probably support “all” the node versions if you want

something like this

"type": "module",
// node <12
"main": "./dist/cjs/index.cjs"
// node 12+
"exports": {
   // ts 4.7+
   "types": "./dist/types/index.d.ts",
   "require": "./dist/cjs/index.cjs",
   "default": "./dist/esm/index.js"
}
// ts < 4.7
"types": "./dist/types/index.d.ts",

and then I guess you would also have a separate umd for CDNs?

0reactions
lucaongcommented, May 30, 2022

Released on NPM as part of v5.0.0-beta2

Read more comments on GitHub >

github_iconTop Results From Across the Web

ECMAScript modules | Node.js v19.3.0 Documentation
Node.js fully supports ECMAScript modules as they are currently specified and provides interoperability between them and its original module format, ...
Read more >
What does it take to support Node.js ESM? – The Guild
ECMAScript modules, also known as ESM, is the official standard format to package JavaScript, and fortunately Node.js supports it .
Read more >
Getting Started with (and Surviving) Node.js ESM
The Node.js ecosystem is shifting towards ESM. We examine what lies in store for application and library authors, learn some of challenges ...
Read more >
Documentation - ECMAScript Modules in Node.js - TypeScript
For the last few years, Node.js has been working to support running ECMAScript modules (ESM). This has been a very difficult feature to...
Read more >
Using ECMAScript modules (ESM) with Node.js
Using official ES module support. Let's start with the first (and official) way provided by Node.js io use ES modules in your Node...
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