[SalesForce] import es6 from an extra javascript file in another component bundle in LWC

I think the documentation completely misses the case when you want to import an extra es6 module from another component bundle. Example, suppose we have the following folder structure.

  • lwc
    • module1
    • module1.html
    • module1.js
    • module1-extra.js
  • module2
    • module2.html
    • module2.js

How to import module1-extra.js into module2.js ?

I tried

import {class1] from 'c/module1'

import {class1] from 'c/module1/module1-extra'

both aproaches failed.

Best Answer

On the Salesforce platform, you can't import a specific file from another module. You can only import the entry file.

If you want module2.js to import some value exported by module1-extra.js this value has to be re-exported in the module entry point.

// module2.js
import { foo } from 'c/module1';

// module1.js
export { foo } from './module1-extra';

// module1-extra.js
export const foo = 'bar';
Related Topic