New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Missing file extension on the import statement #622
Comments
The For better or worse, the TypeScript team has decided that transforming TypeScript to JavaScript does not involve modifying the input file paths. If the environment you are running the code in does not implicitly add an extension, the way to do this with TypeScript is to write the desired file extension in the source itself. So to import Unfortunately this doesn't work with the I'm guessing the way people have been working around this in the meantime is to just not use the |
I'm going to close this because I believe the workaround mentioned above should work. You can also automatically add this extension with a plugin if you'd like (you have to enable bundling and then mark every import as external): bundle: true,
format: 'esm',
plugins: [{
name: 'add-mjs',
setup(build) {
build.onResolve({ filter: /.*/ }, args => {
if (args.importer)
return { path: args.path + '.mjs', external: true }
})
},
}], |
Adding the .js extension explicitely in the .ts files does work, but may lead to other issues eg with Storybook: storybookjs/storybook#15962 The plugin you propose doesn't work in this scenario because you don't want to bundle the "index" file that exposes the ".js" esm. I get errors like this:
It doesn't seem to do what I am looking for, adding ".js" to the "import { Foo } from "./Foo"; So so far the fix seems to be at Storybook level, adding a Webpack config to support ".js" in ".ts files. |
This would only happen if you deleted the
This is Storybook's problem, not esbuild's. The TypeScript team has explicitly decided that this is how TypeScript works. TypeScript takes a philosophical stance that your import paths in TypeScript code are supposed to be written in their final form, and are not supposed to be transformed when converting TypeScript to JavaScript. So if you are compiling TypeScript to ESM files ending in |
Thanks so it confirms that the fix proposed at Storybook level, with resolve-typescript-plugin, is the most appropriate solution. I haven't tested with Jest or other bundlers yet, but at least it works ok in my app with Storybook during development. |
Hi here, # let's find the end of an import statement,
# split the finding into two capture groups,
# combine the two catpure groups and insert '.js'
# there are three capture groups in fact, but the second one (\.\/|\.\.\/)
# is only used for matching the input, not for changing the output
SED_REGEX='s/([\} ]{0,1} from "(\.\/|\.\.\/){1,}[A-Za-z0-9\/]*)(";)/\1.js\3/'
# find all js files within our compiled server code
# and change the relative imports
find [build-output-folder] -type f -name '*.js' -exec sed -ri "$SED_REGEX" {} + For me, it just works, so I'm happy for now. Dynamic imports are currently missing, so there is that. |
We found the Just point it at a directory and it should do the right thing. https://gist.github.com/ds300/f6177171ac673f98f6028799563a06db |
I ended doing something similar, but I'm not happy using a hack. I like |
What is we have the library as |
I just noticed that when building a .ts file, the
import
statement in the output doesn't includeoutExtension
.Assuming that
"type": "module"
👆 src/main.ts
👇 dist/main.mjs
As you notice it's importing from './index' which causes
ERR_MODULE_NOT_FOUND
when runningdist/main.mjs
.According to nodejs docs,
import
keyword should have file extensions.The text was updated successfully, but these errors were encountered: