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

Document more ways to read package.json in ESM #4572

Merged
merged 3 commits into from Jul 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 2 additions & 3 deletions .lintstagedrc.js
@@ -1,5 +1,4 @@
module.exports = {
'.ts': ['eslint --fix --cache'],
'!(test/**/*).js': ['eslint --fix --cache'],
'{test/*,test/*/*,test/*/samples/**/_config}.js': ['eslint --fix --cache']
'*.{ts,js}': ['eslint --fix --cache'],
'*.md': ['prettier --write'],
};
22 changes: 21 additions & 1 deletion docs/01-command-line-reference.md
Expand Up @@ -288,7 +288,27 @@ On the other hand if you are using at least Node 13 and have `"type": "module"`
There are some potential gotchas when using `.mjs` on Node 13+:

- You will only get a default export from CommonJS plugins
- You may not be able to import JSON files such as your `package.json file`. There are two ways to go around this:
- You may not be able to import JSON files such as your `package.json file`. There are four ways to go around this:

- read and parse the JSON file yourself via

```
// rollup.config.mjs
import { readFileSync } from 'fs';

const packageJson = JSON.parse(readFileSync('./package.json'));
...
```

- use `createRequire` via

```
// rollup.config.mjs
import { createRequire } from 'module';
const require = createRequire(import.meta.url);
const packageJson = require('./package.json');
...
```

- run Rollup CLI via

Expand Down