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

Can't figure out how to use in commonjs project #51

Closed
sanderpick opened this issue Oct 10, 2022 · 0 comments · Fixed by #52
Closed

Can't figure out how to use in commonjs project #51

sanderpick opened this issue Oct 10, 2022 · 0 comments · Fixed by #52
Assignees

Comments

@sanderpick
Copy link
Member

I'm trying to use this repo in https://github.com/tablelandnetwork/example-voter. This project uses hardhat, which doesn't support ESM modules w/ Typescript yet. See NomicFoundation/hardhat#3043.

I've tried using require(), no luck. It still thinks it's a module:

Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/sander/github/tablelandnetwork/example-voter/node_modules/@tableland/local/bin/cjs/main.js from /Users/sander/github/tablelandnetwork/example-voter/test/integration/build/test/integration/ask.test.js not supported.
main.js is treated as an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which declares all .js files in that package scope as ES modules.
Instead rename main.js to end in .cjs, change the requiring code to use dynamic import() which is available in all CommonJS modules, or change "type": "module" to "type": "commonjs" in /Users/sander/github/tablelandnetwork/example-voter/node_modules/@tableland/local/package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).

    at Object.<anonymous> (/Users/sander/github/tablelandnetwork/example-voter/test/integration/build/test/integration/ask.test.js:11:15)
    at importOrRequire (/Users/sander/github/tablelandnetwork/example-voter/node_modules/tape/bin/import-or-require.js:14:2)
    at /Users/sander/github/tablelandnetwork/example-voter/node_modules/tape/bin/tape:88:8
    at Array.reduce (<anonymous>)
    at importFiles (/Users/sander/github/tablelandnetwork/example-voter/node_modules/tape/bin/tape:85:27) {
  code: 'ERR_REQUIRE_ESM'
}

I've tried importing /bin/cjs/main.js directly but get this complaint that it's not exported:

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './bin/cjs/main.js' is not defined by "exports" in /Users/sander/github/tablelandnetwork/example-voter/node_modules/@tableland/local/package.json
    at new NodeError (node:internal/errors:371:5)
    at throwExportsNotFound (node:internal/modules/esm/resolve:453:9)
    at packageExportsResolve (node:internal/modules/esm/resolve:729:3)
    at resolveExports (node:internal/modules/cjs/loader:482:36)
    at Function.Module._findPath (node:internal/modules/cjs/loader:522:31)
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:919:27)
    at Function.Module._load (node:internal/modules/cjs/loader:778:27)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (/Users/sander/github/tablelandnetwork/example-voter/test/integration/build/test/integration/setupTest.js:9:19) {
  code: 'ERR_PACKAGE_PATH_NOT_EXPORTED'
}

Might be easiest to just play with my branch: https://github.com/tablelandnetwork/example-voter/tree/sander/local-tableland !

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