Using -r/require flag seems force CJS
See original GitHub issueReproduction
https://github.com/rschristian/uvu-require-reproduction
Fresh WMR app, with the Preact Uvu example (somewhat) copied in. Just with a bit of an alteration to the babel config to instead use the HTM transform that WMR uses (in prod, but this one is easily installable).
Summary of problem
The issue seems to be that when using the require flag (-r
) everything is interpreted to be CJS. preact-iso
is shipped with "type": "module"
, so it’s very clearly ESM, and yet Uvu seems to try to require()
it. Maybe this is user error, but it doesn’t seem like that flag should alter module resolution.
I’ve only just came across this and it’s late here so I’ll need to do more investigation (hopefully) tomorrow.
Error Message
➜ ~/Projects/foobar git:(master) ✗ yarn test
yarn run v1.22.10
$ uvu tests -r esm -r @babel/register -i setup
/home/ryun/Projects/foobar/node_modules/pirates/lib/index.js:1
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /home/ryun/Projects/foobar/node_modules/preact-iso/router.js
require() of ES modules is not supported.
require() of /home/ryun/Projects/foobar/node_modules/preact-iso/router.js from /home/ryun/Projects/foobar/public/header.jsx is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename router.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /home/ryun/Projects/foobar/node_modules/preact-iso/package.json.
at Module._extensions..js (internal/modules/cjs/loader.js:1080:13)
at Object.newLoader [as .js] (/home/ryun/Projects/foobar/node_modules/pirates/lib/index.js:104:7)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
Tool Versions
Node: v14.16.0 Yarn: v1.22.10
Issue Analytics
- State:
- Created 2 years ago
- Comments:10 (10 by maintainers)
Top Results From Across the Web
Command-line parameter to force "module" instead ... - GitHub
I'm aware that I can name a file with .mjs or set "type": "module" in package.json. I'm also aware of the --input-type=module CLI...
Read more >detect whether ES Module is run from command line in Node
It looks like there is a documented way to do this now: if (require.main === module) { console.log('executed directly'); . . . }....
Read more >Getting Started with (and Surviving) Node.js ESM
This field controls and changes a lot of existing behavior as to how Node.js resolves a file when you do something like import...
Read more >Using ES Modules (ESM) in Node.js: A Practical Guide (Part 2)
ESM is ready for use in Node.js. This guide shows you how, and how to avoid all the small gotchas. The guide covers...
Read more >Command-line API | Node.js v19.3.0 Documentation
The program was started with a command-line flag that forces the entry point to be loaded with ECMAScript module loader. The file has...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Makes sense.
I have gotten lucky and avoided manually dealing with transforms up to this point, so going through this for the first time and learning about the of troubled areas is certainly interesting
Yes,
-r
with uvu forces CommonJS version ofuvu/run
. This is because require hooks are exclusively a CommonJS thing