Does ES6 import/export need ".js" extension?
ES6 import/export need “.js” extension. There are clear instructions in node document:
- Relative specifiers like './startup.js' or '../config.mjs'. They refer to a path relative to the location of the importing file. The file extension is always necessary for these.
- This behavior matches how import behaves in browser environments, assuming a typically configured server.
https://nodejs.org/api/esm.html#esm_import_expressions
No, modules don't care about extensions. It just needs to be a name that resolves to a source file.
In your case, http://localhost/bla/src/drawImage
is not a file while http://localhost/bla/src/drawImage.js
is, so that's where there error comes from. You can either add the .js
in all your import statements, or configure your server to ignore the extension, for example. Webpack does the same. A browser doesn't, because it's not allowed to rewrite urls arbitrarily.
The extension is part of the filename. You have to put it in.
As proof of this, please try the following:
- rename file to
drawImage.test
- edit
index.js
to contain'./drawImage.test'
Reload, and you'll see the extension js
or test
will be completely arbitrary, as long as you specify it in the export
.
Obviously, after the test revert to the correct/better js
extension.