question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`import` fails to resolve when using `esbuild` build tool

See original GitHub issue

Is your feature request related to a problem? Please describe.

When using the esbuild build tool, the import statement reports an error that cannot be resolved.

E.g:

import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls';

This is because of the following configuration:

"exports": {
  "./examples/jsm/*": "./examples/jsm/*"
}

Although tools like rollup, webpack, etc. may be normal, obviously esbuild respects node specification standards more.

According to the node specification, in order for the esbuild tool to work properly, the file extension needs to be added:

import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js';

Obviously when this happens, many people need to spend as much time as I do to analyze the cause of the problem.

In fact, the following configuration can also solve the problem:

"exports": {
  "./examples/jsm/*": "./examples/jsm/*.js" 
}

In order to improve development efficiency (compilation speed), esbuild should have been widely used, can this problem be solved?

_see also https://github.com/evanw/esbuild/issues/2518_

Describe the solution you’d like

  1. For files of the same type (such as .js), change the exports configuration in package.json, for example
"exports": {
  "./examples/jsm/*": "./examples/jsm/*.js"
}
  1. For different categories of files contained in a folder, multiple configurations can be written, or it is clearly mentioned in the documentation that developers should write extensions when importing?
"exports": {
  "./examples/jsm/*.js": "./examples/jsm/*.js",
  "./examples/jsm/*.png": "./examples/jsm/*.png"
}

Describe alternatives you’ve considered

Additional context

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:32 (14 by maintainers)

github_iconTop GitHub Comments

6reactions
donmccurdycommented, Sep 3, 2022

I thought omitting the .js extension on imports was mostly a TypeScript-ism, is it used outside TS?

I don’t think we should be doing that anywhere within this project, including examples…

3reactions
gkjohnsoncommented, Sep 3, 2022

If you’d like esbuild to be more flexible in its ability to resolve package paths I recommend making an issue or PR to the esbuild project rather than one here. But either way it is not common for packages to export files in the style you have recommended. And as you’ve mentioned this kind of export can lead to file ambiguity. It’s best to explicitly import the files you want to your project. You can see that even react-dom does not export files in a way that affords dropping the file extension.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ESBuild Fails to Resolve ESM Module from node_modules ...
esbuild @0.11.23 is building a module that includes this code... import * as tf from '@tensorflow/tfjs'; import { loadGraphModel } from '@ ...
Read more >
API - ESBuild
#Inject. Supported by: Build. This option allows you to automatically replace a global variable with an import from another file. This can be...
Read more >
esbuild build does not bundle the public directory
I decided to use esbuild to bundle my express API. So far all is good, except that the public directory which holds the...
Read more >
Some notes on using esbuild - Hacker News
problem 2: I don't understand frontend build tools ... With Vite at least I was able to alias libraries to same import thus...
Read more >
Getting started with esbuild - LogRocket Blog
If you don't use the bundle option, esbuild will just import the ... cuts through the noise to proactively resolve issues in your...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found