Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use import statement instead of require #899

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ Once you have [installed a package][install-pkg] in `node_modules`, you can use

### Node.js module

If you are creating a Node.js module, you can use a package in your module by passing it as an argument to the `require` function.
If you are creating a Node.js module, you can use a package in your module by importing it with the `import` statement.

```javascript
var lodash = require('lodash');
// index.mjs
import chalk from 'chalk';

var output = lodash.without([1, 2, 3], 1);
console.log(output);
console.log(chalk.blue('Hello world!'));
```

### package.json file
Expand All @@ -36,7 +36,7 @@ To use a scoped package, simply include the scope wherever you use the package n
### Node.js module

```js
var projectName = require("@scope/package-name")
import packageName from "@scope/package-name";
```

### package.json file
Expand All @@ -51,15 +51,21 @@ In `package.json`:
}
```

## Resolving "Cannot use import statement outside a module" error

The error message gives you two options to handle this,
- Add `"type": "module"` field to your `package.json`, or
- Use `.mjs` file extension instead of `.js`.

## Resolving "Cannot find module" errors

If you have not properly installed a package, you will receive an error when you try to use it in your code. For example, if you reference the `lodash` package without installing it, you would see the following error:
If you have not properly installed a package, you will receive an error when you try to use it in your code. For example, if you reference the `chalk` package without installing it, you would see the following error:

```
module.js:340
throw err;
^
Error: Cannot find module 'lodash'
Error: Cannot find module 'chalk'
```

- For scoped packages, run `npm install <@scope/package_name>`
Expand Down