I'm trying to use esm along with regular .js files, running into the following error:
import * as api from 'actions/apis/accounts';
Error [MODULE_NOT_FOUND]: Module 'actions/apis/accounts'not found by `import` in '.', but would be found by `require` at '/Users/pgilad/repos/gui/app/actions/apis/accounts.js'
I use NODE_PATH=./app when starting the app, so I think it's not being respected (but works with just transpiling using babel + webpack)
Hi @pgilad!
This is inline with Nodes ESM rules.
Oh wow, thanks for the update, did not know that!
I currently don't have that toggleable but I'll make it so that if you're using "cjs":true that you can use NODE_PATH and others. Watch this issue for updates on when it lands.
Patched https://github.com/standard-things/esm/commit/8ab34362edcac1dcec1ca7b708ae9d1465d18d08 and available v0.3.0+.
Hello. I'm using NODE_PATH for shortening imports in testing envrionment.
@jdalton what my migration path on v3? I used to use:
"@std/esm": "cjs"
Now I've installed v3 and my config turned into:
"esm":
{
"mode": "auto",
"cjs": { "vars": true, "paths": true, "namedExports": true }
}
but that seemes not enough.
I use $ mocha -r test/patch -r esm -R, where patch modifies NODE_PATH, then esm is in play. This had been worked on pre-releases of esm. Now I got error like Error: Cannot find module 'src/Context' where Context.js is ESM-like.
Hi @StreetStrider!
You don't need that config as the default zero config has those options enabled.
Are you using .mjs files? Because...
:lock:
.mjsfiles are limited to basic functionality without support foresmoptions.
@jdalton I'm using .js files. Docs say mode = auto is enough for them to be discovered by esm, am I still missing something? I believe, usage of .js files is the only deviation that present in my project.
@StreetStrider
It looks like you've hit a regression/bug. Mind pointing me to a repro repo so I can dig in and fix it?
Ok digging in. Your commit before was v0.15.0 from November. Can you check with v0.25.2.
Update:
Ok found the issue. In the early versions of std/esm, so say v0.15.0, I wasn't aligning 馃挴 with the builtin behavior of setting process.env.NODE_PATH. The Node way is to do:
let { resolve } = require('path')
process.env.NODE_PATH = resolve(__dirname, '..')
require('module')._initPaths()
The require('module')._initPaths() is the new bit. Now, if you do that it will work.
v0.25 not working as well.
The require('module')._initPaths() is the new bit.
Got it! Will do.
node/lib/module.js is looking better then I saw it last time, but still got this magic _init phase.
@jdalton I've ported fix to real project with "esm": true, confirmed to be working. Thank you for help.
Most helpful comment
I currently don't have that toggleable but I'll make it so that if you're using
"cjs":truethat you can useNODE_PATHand others. Watch this issue for updates on when it lands.