Skip to content

Commit 22f731c

Browse files
authored
docs: add import syntax with module example (#1831)
Specify `import` alongside `require()` syntax
1 parent 7b88fdd commit 22f731c

1 file changed

Lines changed: 11 additions & 2 deletions

File tree

content/packages-and-modules/introduction-to-packages-and-modules/about-packages-and-modules.mdx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,22 @@ Installing any package directly from git will not install [git submodules](https
4343

4444
## About modules
4545

46-
A **module** is any file or directory in the `node_modules` directory that can be loaded by the Node.js `require()` function.
46+
A **module** is any file or directory in the `node_modules` directory that can be loaded by the Node.js `require()` or `import` syntax.
4747

4848
To be loaded by the Node.js `require()` function, a module must be one of the following:
4949

5050
- A folder with a `package.json` file containing a `"main"` field.
5151
- A JavaScript file.
5252

53+
To use the `import` syntax, a module should also include `"type": "module"` in its `package.json` file:
54+
55+
```json
56+
{
57+
"name": "my-package",
58+
"type": "module"
59+
}
60+
```
61+
5362
<Note>
5463

5564
**Note:** Since modules are not required to have a `package.json` file, not all modules are packages. Only modules that have a `package.json` file are also packages.
@@ -59,7 +68,7 @@ To be loaded by the Node.js `require()` function, a module must be one of the fo
5968
In the context of a Node program, the `module` is also the thing that was loaded _from_ a file. For example, in the following program:
6069

6170
```
62-
var req = require('request')
71+
const req = require('request')
6372
```
6473

6574
The `req` variable refers to the `request` module returned by the `require()` function.

0 commit comments

Comments
 (0)