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.

I can't load through commonjs in Node

See original GitHub issue

I tried a number of ways to require it but to no avail, I literally can’t use file-type on Node because of this. It looks like you just need to remove the module prop from the package.json.

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /Users/.../node_modules/file-type/index.js ... Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /Users/.../node_modules/file-type/package.json.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
Borewitcommented, Jan 14, 2022
/**
 * Import 'file-type' ES-Module in CommonJS Node.js module
 */
(async () => {
  const { fileTypeFromFile } = await import('file-type');

  const type = await fileTypeFromFile('fixture/fixture.gif');
  console.log(type);
})();

Full working example: https://github.com/Borewit/file-type-example/tree/master/commonjs

0reactions
sindresorhuscommented, Jan 18, 2022

You are of course free to dislike ESM, but that doesn’t mean it won’t happen.

Dual packages come with their own downsides: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c#gistcomment-3850849

I have no plans to switch any of my packages to be dual.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Node Modules at War: Why CommonJS and ES ... - Code Red
All of the “sibling” scripts in the ES module graph download in parallel, but they execute in order, guaranteed by the loader specification....
Read more >
Using ES modules with CommonJS modules in Node.js
Mixing ES modules and CommonJS modules · You can only use import and export in an ES module. Specifically, this means you can...
Read more >
node: getting error importing CommonJS module however I try ...
I have a local node module, @em/inVis in my company whose index.d.ts file looks as follows: import UWeb from ".
Read more >
CommonJS vs. ES modules in Node.js - LogRocket Blog
Learn about the differences between CommonJS and ES modules when using them in Node.js applications to organize software code.
Read more >
How the module system, CommonJS & require works
We can allow Node.js to manage the modules life cycle by using hard coded module loading. It organizes your packages in an intuitive...
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