Skip to content

fix(build): use __dirname to resolve package.json in postcss.config.js and docsSource/index.js #1929

Open

Description

@sujlcodes

Describe The Bug

Both postcss.config.js and docsSource/index.js read package.json with a relative string path:

// postcss.config.js line 2
const {homepage, version, author, animateConfig} = JSON.parse(fs.readFileSync('package.json'));

// docsSource/index.js line 4
const {version} = JSON.parse(fs.readFileSync('package.json'));

In Node.js, fs.readFileSync('package.json') resolves relative to the current working directory (process.cwd()). When PostCSS or the doc generator is invoked from a directory other than the repository root (e.g., cd docsSource && node index.js or cd source && npx postcss animate.css --config ../postcss.config.js), or from a parent monorepo workspace, Node crashes with ENOENT.

Steps To Reproduce

  1. Open a terminal.
  2. Run cd docsSource.
  3. Run node index.js.
  4. See error:
Error: ENOENT: no such file or directory, open '.../docsSource/package.json'
    at Object.openSync (node:fs:561:18)
    at Object.readFileSync (node:fs:445:35)
    at Object.<anonymous> (.../docsSource/index.js:4:33)


### Expected Behavior

Scripts and PostCSS configuration should resolve `package.json` using `path.join(__dirname, ...)` so they can be executed from any working directory.

### Screenshots

_No response_

### Desktop

- OS: Windows, macOS, Linux
- Node Version: v14+ (tested on Node v18, v20, v24)

### Smartphone

_No response_

### Additional Context

**Suggested fix:**
In `postcss.config.js`:
```javascript
const path = require('path');
const {homepage, version, author, animateConfig} = JSON.parse(
  fs.readFileSync(path.join(__dirname, 'package.json'))
);

In docsSource/index.js:

const {version} = JSON.parse(
  fs.readFileSync(path.join(__dirname, '../package.json'))
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions