0

When importing modules in JavaScript on client side, should the module extension be .js or .mjs?

import {myFunction, myVar} from './my_module.js'
// Or
import {myFunction, myVar} from './my_module.mjs'

The following question (What is the difference between .js and .mjs files?) is really interresting, but is focused on server-side (node.js). What about client side?

Fifi
  • 3,360
  • 2
  • 27
  • 53
  • it's not a thing on the client side - use `.js` - besides, many HTTP servers would not send the right mime type for `.mjs` unless you configured them to – Jaromanda X Jan 12 '20 at 12:12
  • Exactly the answer I expected, one more time, Thank you Jaromanda. I'll upvote your answer if you post it. – Fifi Jan 12 '20 at 12:17

1 Answers1

1

There's no such thing as a file extension when you are dealing with HTTP. The Content-Type response header is used instead (and should be application/javascript just like any other JS).

You'll probably want use the .js file extension when serving static files since HTTP servers will tend to recognise it as JS so that is a convenient way to set the right Content-Type automatically.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335