Esm: NODE_PATH not respected?

Created on 13 Aug 2017  路  12Comments  路  Source: standard-things/esm

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)

invalid

Most helpful comment

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.

All 12 comments

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.

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: .mjs files are limited to basic functionality without support for esm options.

@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?

@jdalton repro, repo.zip. There is also a commit before where things work for comparison.

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Mensu picture Mensu  路  3Comments

dnalborczyk picture dnalborczyk  路  3Comments

bensampaio picture bensampaio  路  3Comments

greggb picture greggb  路  3Comments

kherock picture kherock  路  3Comments