Skip to content

Commit

Permalink
Document more ways to read package.json in ESM (#4572)
Browse files Browse the repository at this point in the history
* Document more ways to read package.json in ESM

* Fix linting

Co-authored-by: Lukas Taegert-Atkinson <lukastaegert@users.noreply.github.com>
Co-authored-by: Lukas Taegert-Atkinson <lukas.taegert-atkinson@tngtech.com>
  • Loading branch information
3 people committed Jul 15, 2022
1 parent b7e451c commit f019fae
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
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

0 comments on commit f019fae

Please sign in to comment.