jprichardson / node-fs-extra

Node.js: extra methods for the fs object like copy(), remove(), mkdirs()
MIT License
9.43k stars 775 forks source link

Don't use dynamic require to export modules #1002

Closed 1aron closed 1 year ago

1aron commented 1 year ago

Source: ( https://github.com/jprichardson/node-fs-extra/blob/master/lib/index.js )

module.exports = {
  // Export promiseified graceful-fs:
  ...require('./fs'),
  // Export extra methods:
  ...require('./copy'),
  ...require('./empty'),
  ...require('./ensure'),
  ...require('./json'),
  ...require('./mkdirs'),
  ...require('./move'),
  ...require('./output-file'),
  ...require('./path-exists'),
  ...require('./remove')
}

Exporting modules using the above way will make the build tool unable to bundle, and the module will not be found.

Screenshot 2023-03-30 at 8 47 01 PM

A safer way is recommended:

const fs = require('./fs')
const copy = require('./copy')
const empty = require('./empty')
const ensure = require('./ensure')
const json = require('./json')
const mkdirs = require('./mkdirs')
const move = require('./move')
const outputFile = require('./output-file')
const pathExists = require('./path-exists')
const remove = require('./remove')

module.exports = {
  ...fs,
  ...copy,
  ...empty,
  ...ensure,
  ...json,
  ...mkdirs,
  ...move,
  ...outputFile,
  ...pathExists,
  ...remove
}
RyanZim commented 1 year ago

What build tool is this?

1aron commented 1 year ago

The ESBuild example is bundled correctly. My project is a bit complicated, and only fs-extra throws an error that the ./fs module cannot be found, I'll find a way to provide a minimal reproduction and temporarily close this issue.