Esm: Can't import with multiple files

Created on 18 Jan 2019  路  3Comments  路  Source: standard-things/esm

I have index.js with:

import a from './lib/a'

And inside folder lib I have two file a.js and b.js, now in a.js I do:

import b from '/b'

// I also try both:

import b from './lib/b'

// And

import b from 'b'

But run node -r esm index.js false with error:

Error: Cannot find module 'b'

How can I import b.js from a.js and import a.js from index.js and run index.js with esm?

question

All 3 comments

seems like you forgot the leading dot in './b' otherwise the module loader tries to load from the top level of the project.

// lib/a.js
import b from './b'

Hi @stpham-net!

  • import b from 'b' is a bare import and will try to resolve 'b' as a npm package.
  • import b from '/b' will try to resolve b.js from the root of your system _(assuming *nix based)_

As @dnalborczyk points out import b from './b' is a relative import and should work for you.

Thanks to @dnalborczyk and @jdalton!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

OmgImAlexis picture OmgImAlexis  路  3Comments

ericelliott picture ericelliott  路  3Comments

bensampaio picture bensampaio  路  3Comments

clock157 picture clock157  路  3Comments

Mensu picture Mensu  路  3Comments