Function getPackageType

    • Attempts to traverse from filepath to basepath attempting to access type field of package.json. The type is returned if it is set in the found package.json otherwise commonjs is returned.

      Note: With only filepath set this function only reliably returns a positive result when there are no intermediary package.json files in between a supposed root and path. If provided with malformed data or there is any error / edge case triggered then 'commonjs' by default will be returned.

      Traversal stops at the first valid package.json file as this is how Node works. If the first found package.json does not have a type field then commonjs is returned.

      Parameters

      Returns string

      Type of package - 'module' for ESM otherwise 'commonjs'.