Eslint-plugin-import: Destructuring a default export fails "not found" import/named

Created on 17 Sep 2018  路  6Comments  路  Source: benmosher/eslint-plugin-import

// utils/helloworld.js
const helloWorld = () => console.log("hello world")
export default {helloWorld}
// index.js
import {helloWorld} from 'utils/helloworld'

error is as follows helloWorld not found in utils/helloworld (import/named)

question

Most helpful comment

That's to be expected; named imports are not destructuring.

You either need import foo from 'path'; const { helloWorld } = foo;, or export { helloWorld }, or export default helloWorld; and import helloWorld from 'path'.

All 6 comments

That's to be expected; named imports are not destructuring.

You either need import foo from 'path'; const { helloWorld } = foo;, or export { helloWorld }, or export default helloWorld; and import helloWorld from 'path'.

Destructuring a default export on import is bad practice?

@hammadzz it鈥檚 not that; it鈥檚 that it doesn鈥檛 work, because that syntax is not destructuring. Named exports are not just properties on the default export, they鈥檙e entirely separate things.

@ljharb I have been using it and the project runs fine. Unless I am wrong this is how destructuring works and I am just applying it on importing a default export which is an object. Not only does it run but Webstorm can pickup on it. So it is valid. Just this plugin complains.

You are wrong, that鈥檚 not how the language works. Babel鈥檚 interoperability can mean that named imports from a CJS module can behave like destructuring, but you鈥檙e importing from an ESM module - there is no relationship between the default and a named export.

I assure you, it鈥檚 not valid at runtime (altho the syntax is of course valid). It won鈥檛 work in production.

@ljharb okay thank you

Was this page helpful?
0 / 5 - 0 ratings

Related issues

msuntharesan picture msuntharesan  路  29Comments

nevir picture nevir  路  40Comments

thewilkybarkid picture thewilkybarkid  路  36Comments

yamov picture yamov  路  29Comments

kirill-konshin picture kirill-konshin  路  22Comments