Skip to content

Commit 27a06eb

Browse files
committed
feat: Split and add to JS modules
1 parent f8d3449 commit 27a06eb

File tree

4 files changed

+130
-69
lines changed

4 files changed

+130
-69
lines changed

cheatsheets/javascript/node/modules.md

Lines changed: 0 additions & 69 deletions
This file was deleted.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# CommonJS
2+
3+
The newer ES Modules syntax is preferred over this style.
4+
5+
Use `module.exports` to export and `require` to import a module.
6+
7+
8+
## Import installed package
9+
10+
```javascript
11+
const request = require('request');
12+
```
13+
14+
15+
## Local modules
16+
17+
### Name exports
18+
19+
Create a variable or function or class, then at the end of the script typically you export it.
20+
21+
- `foo.js`
22+
```javascript
23+
function foo() {
24+
console.log("Hello");
25+
}
26+
27+
exports.foo = 'foo';
28+
// Or
29+
module.exports.foo = 'foo';
30+
// Result:
31+
// { foo: 'foo'}
32+
```
33+
34+
Select objects in the module.
35+
36+
```javascript
37+
const { bar } = require('./foo')
38+
// Or, less common.
39+
const bar = require('./foo').bar;
40+
```
41+
42+
### Default exports
43+
44+
- `foo.js`
45+
```javascript
46+
function foo() {
47+
console.log("Hello");
48+
}
49+
50+
module.exports = 'foo';
51+
```
52+
53+
Import the default object from module with any name you want.
54+
55+
```javascript
56+
const foo = require('./foo');
57+
```
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# ES modules
2+
3+
As of ES6 (ES2015), JavaScript `supports` a native module format. It uses the `export` and `import` keywords.
4+
5+
6+
## Import installed package
7+
8+
Depending on the structure of the module.
9+
10+
```javascript
11+
import * as foo from 'foo';
12+
```
13+
Or
14+
```javascript
15+
import { foo } from 'foo';
16+
```
17+
18+
19+
## Local modules
20+
21+
Note you must set this up otherwise you'll get an error.
22+
23+
- `package.json`
24+
```json
25+
{
26+
"type": "module"
27+
}
28+
```
29+
30+
### Named exports
31+
32+
- `foo.js`
33+
```javascript
34+
function foo() {
35+
console.log("Hello");
36+
}
37+
38+
export { foo };
39+
```
40+
- `foo.js` alternative.
41+
```javascript
42+
export function foo() {
43+
console.log("Hello");
44+
}
45+
```
46+
47+
```javascript
48+
import { foo } from './foo';
49+
```
50+
51+
### Default exports
52+
53+
- `foo.js`
54+
```javascript
55+
function foo() {
56+
console.log("Hello");
57+
}
58+
59+
export default foo;
60+
```
61+
62+
```javascript
63+
import foo from './foo';
64+
```
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
layout: listing
3+
description: How to export and import modules in Node
4+
---
5+
# Modules
6+
7+
8+
- [Modules](https://nodejs.org/api/modules.html) in the Node docs
9+
- [Different module formats](https://www.sitepoint.com/understanding-module-exports-exports-node-js/)

0 commit comments

Comments
 (0)