I got "Cannot find module" with my @lib on import. Is there way to fix this?
Package.json
{...
"scripts": {
"dev": "DB_HOST=localhost:27017 nodemon --trace-warnings -w src --exec \"node src/index.js\"",
"start": "node src/index.js",
"test": "eslint src"
},
"@std/esm": "cjs",
"_moduleAliases": {
"@root": "./src",
"@api": "./src/api",
"@lib": "./src/lib",
"@middleware": "./src/middleware",
"@models": "./src/models"
}
...}
Index.js
require = require('@std/esm')(module)
module.exports = require('./app.mjs')
app.mjs
require('module-alias/register');
import http from 'http';
...
Hi @ChuntheQhai!
Can you create a simple repro repo for me to look at?
@jdalton
Here's my repo for ESM with module alias:
https://github.com/ChuntheQhai/simple-esm-module-alias
Okay @ChuntheQhai!
Making this work is doable. I'm going to make it work with the option "@std/esm": "cjs". The reason it doesn't work at the moment is because of two things. The first is that module-alias assumes the module system is referencing Module._nodeModulePaths and Module._resolveFilename. The second is that it assumes Module is of require("module"). So for my part when "@std/esm":"cjs" I'll reference Module methods directly instead of cherry-picked imports of them. For the other I'll make a PR to module-alias so that it doesn't use require("module") directly and will get it from the module.constructor. This way it will work with our module system (and others).
@ChuntheQhai / @jdalton - did this ever address the issue? I'm trying to make jest work with esm in my setup and it seems having module alias would solve another one of the problems (the last problem would be all those .css / .scss imports)...
Okay, I updated to latest @std/esm and it WORKS. It seems that the latest esm versions has regressed this behavior...
did this ever address the issue
Yes.
It seems that the latest esm versions has regressed this behavior...
Yep. I was just going to post that. It looks like I regressed it when I tried to cache a bit more across loader instances. I'll fix that up tonight 😋
Update:
v3.0.11 is released 🎉
Okay I see this is fixed for cjs 👍
However, I'm trying to update that simple repo from @ChuntheQhai with the latest esm, but it wouldn't work since the app.mjs is importing it as esm... wondering how I can get the same behavior as @std/esm's setting for package.json specifying "esm": "cjs" - I tried {mode: 'auto', cjs: true} but didn't work.
.mjs is locked down intentionally. Since it's the Node-way™, and they haven't fully written its support yet, we can't responsibly expand its functionality. You'll not be able to enable it with .mjs. For things to "just work" you'll want to stick with ESM in .js for now.
I wanted to follow up on this - I've forked the simple repro and made it work:
https://github.com/kenotron/simple-esm-module-alias
Such a common usage, and zero explanation in the document.
const esmRequire = require('esm')(module, { cjs: { topLevelReturn: true }})
esmRequire('module-alias/register')
require('module-alias').addAliases({
'@': __dirname
})
esmRequire('./logic/demo')
For me that worked
Added "@std/esm": "cjs" to package.json
nodemon -r @std/esm -r module-alias/register ./app/server.mjs
Most helpful comment
I wanted to follow up on this - I've forked the simple repro and made it work:
https://github.com/kenotron/simple-esm-module-alias