Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

version v10.2.1 breaks CJS #36

Closed
boneskull opened this issue Mar 18, 2024 · 10 comments
Closed

version v10.2.1 breaks CJS #36

boneskull opened this issue Mar 18, 2024 · 10 comments

Comments

@boneskull
Copy link

boneskull commented Mar 18, 2024

Describe the Bug

After 3ea4ec5, the package is not resolvable via CJS.

Steps to Reproduce

  1. Build project
  2. Execute node -e "require('.')" from project root
  3. Error:
    node:internal/modules/cjs/loader:1147
    throw err;
    ^
    
    Error: Cannot find module '.'
    Require stack:
    - /Users/boneskull/projects/nikku/didi/[eval]
      at Module._resolveFilename (node:internal/modules/cjs/loader:1144:15)
      at Module._load (node:internal/modules/cjs/loader:985:27)
      at Module.require (node:internal/modules/cjs/loader:1235:19)
      at require (node:internal/modules/helpers:176:18)
      at [eval]:1:1
      at runScriptInThisContext (node:internal/vm:144:10)
      at node:internal/process/execution:109:14
      at [eval]-wrapper:6:24
      at runScript (node:internal/process/execution:92:62)
      at evalScript (node:internal/process/execution:123:10) {
    code: 'MODULE_NOT_FOUND',
    requireStack: [ '/Users/boneskull/projects/nikku/didi/[eval]' ]
    }
    

Expected Behavior

The command above should exit with a non-zero exit code.

Environment

  • Host (Browser/Node version), if applicable: [e.g. MS Edge 18, Chrome 69, Node 10 LTS] Node.js v20.10.0
  • OS: [e.g. Windows 7] macOS
  • Library version: [e.g. 2.0.0] latest
@boneskull boneskull added the bug label Mar 18, 2024
@nikku
Copy link
Owner

nikku commented Mar 18, 2024

@boneskull thanks for the report.

How is this a valid usage of the project? Why would you expect to require('.') within diagram-js?

@nikku nikku added the question label Mar 18, 2024
@boneskull
Copy link
Author

err, that's just the easiest way to reproduce. use require('didi') somewhere.

@nikku
Copy link
Owner

nikku commented Mar 18, 2024

You mean this? It works on my machine, so please provide concrete steps to reproduce:

~/camunda/projects/bpmn.io/.other/foo
> npm install didi

up to date, audited 3 packages in 512ms

found 0 vulnerabilities

~/camunda/projects/bpmn.io/.other/foo
> npm 

~/camunda/projects/bpmn.io/.other/foo
> cat package.json 
{
  "dependencies": {
    "didi": "^10.2.1"
  }
}

~/camunda/projects/bpmn.io/.other/foo
> node -e "console.log(require('didi'))"
{
  Injector: [Function: Injector],
  annotate: [Function: annotate],
  parseAnnotations: [Function: parseAnnotations]
}

~/camunda/projects/bpmn.io/.other/foo
> node --version
v20.11.1

@boneskull
Copy link
Author

Yeah, this is weird. You're right--that works. I don't understand why require('.') doesn't, though. What I was ultimately trying to fix was the error that I get from TS, and I got sidetracked:

/path/to/some-file.ts:1:24 - error TS1479: The current file is a CommonJS module whose imports will produce 'require' calls; however, the referenced file is an ECMAScript module and cannot be imported with 'require'. Consider writing a dynamic 'import("didi")' call instead.
  To convert this file to an ECMAScript module, change its file extension to '.mts', or add the field `"type": "module"` to '/path/to/package.json'.

1 import {Injector} from 'didi';
                         ~~~~~~

It appears that might be fixable as of TS v5.3 via import attributes (alas I am still on v5.2), but it is also fixable by the lib author using something like tshy. I am not sure the levers tshy pulls to avoid the above error, but it might be worth looking into if you intend to ship a dual module. I am doing this in snapshot-fs if you wish to see a relatively straightforward example.

@boneskull
Copy link
Author

That said, if you put main back in, it will work as expected when required as a relative path:

{
  "main": "./dist/index.cjs"
}

Not sure your reasons for removing it.

@nikku
Copy link
Owner

nikku commented Mar 18, 2024

@boneskull I've seen a bunch of bundlers having issues with main being a CJS module, not the ESM export. Hence I'm looking into migrating my libraries into ESM only and/or providing dual exports via the exports package.json field. At some point I plan to drop CJS support all together as we've seen many do in the larger eco-system.

I still do not understand under which circumstances you get typescript errors. You should not get them in my tests. But if "main": "..." fixes the situation for you (and it was in fact a breaking change to remove it), please provide me a setup I can reproduce this with and I'm happy to restore the past (if it does not break the future).

@nikku
Copy link
Owner

nikku commented Mar 18, 2024

Consider to move your module resolution strategy in typescript to NodeNext. That should be aware of exports.

@nikku
Copy link
Owner

nikku commented Mar 19, 2024

I found another case where removing main and module would break downstream libraries. I'll go ahead and revert that change. Better safe than sorry.

@nikku nikku reopened this Mar 19, 2024
@nikku nikku closed this as completed in e44aa44 Mar 19, 2024
@boneskull
Copy link
Author

boneskull commented Mar 20, 2024

Yeah, it breaks w/ older versions of node as well. AFAICT there's really no reason not to have a main.

The resolution strategy is using NodeNext, but it doesn't help with the TS errors. It's just an import {Injector} from 'didi' in a TS source file where TS is compiling to CJS. If I was compiling to ESM, I wouldn't have the error.

@boneskull
Copy link
Author

Anyway, please let me know if you want to get to the bottom of it, and I can create a minimal repo to reproduce.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants