snippetjavascriptTip
JavaScript modules Cheat Sheet
Viewed 0 times
javascriptcheatsheetmodules
Problem
```js title="environment.js"
export const key = 'this-is-a-secret';
js title="index.js"
import { key } from 'environment';
js title="environment.js"
export const key = 'this-is-a-secret';
js title="index.js"
import { key } from 'environment';
js title="environment.js"
Solution
- Named exports use a name.
- A module can have any number of named exports.
- Import and export name should be the same.
- Importing requires `{}`.
## Default exportsjs title="index.js"
import { key } from 'environment';
js title="environment.js"
const environment = {
key: 'this-is-a-secret',
port: 8000
Code Snippets
- Named exports use a name.
- A module can have any number of named exports.
- Import and export name should be the same.
- Importing requires `{}`.
## Default exports- Default exports expose a default value, use the `default` keyword.
- A module can only have one default export.
- Import name can be anything.
- Importing does not require `{}`.
## Default + named- Default and named exports can be mixed.
- Rules about number of exports and naming conventions apply as before.
- Import rules apply as before, can be mixed if necessary.
## Export listContext
From 30-seconds-of-code: module-cheatsheet
Revisions (0)
No revisions yet.