google / zx

A tool for writing better scripts
https://google.github.io/zx/
Apache License 2.0
42.92k stars 1.09k forks source link

Could not resolve "zx/globals.cjs" #869

Closed uladzimirdev closed 2 months ago

uladzimirdev commented 2 months ago

I have a build step that transpiles code for nodejs into cjs format to use it in github actions.

as a build tool I use esbuild like

esbuild src/index.ts --bundle --outfile=dist/index.cjs --platform=node --target=node18

it works fine until the moment I need to export a fucntion from index.ts - I see the next error

Could not resolve "zx/globals.cjs"

      2 │ import "zx/globals.cjs";
        ╵        ~~~~~~~~~~~~~~~~

  The path "./globals.cjs" is not exported by package "zx":

    node_modules/zx/package.json:24:13:
      24 │   "exports": {
         ╵              ^

  You can mark the path "zx/globals.cjs" as external to exclude it from the
  bundle, which will remove this error.

may it be a problem with exports configuration?

in GitHub actions I use it inside the github-actions/scripts section

- uses: actions/github-script@v7
        with:
          script: | # js
            const { setMilestoneForCommits } = require('${{ github.workspace }}/release/dist/index.cjs');

where release/dist is a folder from esbuild step.

it may be a wrong configuration though

antongolub commented 2 months ago

zx exposes zx/globals entry point, which will be resolved as ESM via import, and as CJS module when the require API is used.

import 'zx/globals'   // esm
require('zx/globals') // cjs

https://github.com/google/zx/blob/main/package.json#L31

uladzimirdev commented 2 months ago

@antongolub afaik bundlers like esbuild, webpack etc are able to read exports part of package.json and load correct file based on the target of the builds (esm/cljs/etc), so it seems to be a valid error

antongolub commented 2 months ago

But how did zx/globals become zx/globals.cjs? Where was the ext attached? Could you provide a full demo ref/example?

uladzimirdev commented 2 months ago

@antongolub upgrade to 8.1.4 (and even 8.1.3) fixed the issue, sorry for bothering

(seems I updated wrong package.json to 8.1.4 during my tests)

8.1.2 produced that output

image