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

Named export 'buf' not found in module 'crc-32' #6034

Closed
1 task done
sharifzadesina opened this issue Apr 21, 2023 · 4 comments
Closed
1 task done

Named export 'buf' not found in module 'crc-32' #6034

sharifzadesina opened this issue Apr 21, 2023 · 4 comments
Assignees
Labels
4.x 4.0 related Bug Addressing a bug

Comments

@sharifzadesina
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Current Behavior

Hello,

As of RC.1 I am getting this error below, it seems you are trying to import a CommonJS module with named exports and it fails.

import { buf } from 'crc-32';
         ^^^
SyntaxError: Named export 'buf' not found. The requested module 'crc-32' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from 'crc-32';
const { buf } = pkg;

    at ModuleJob._instantiate (node:internal/modules/esm/module_job:123:21)
    at async ModuleJob.run (node:internal/modules/esm/module_job:189:5)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:530:24)
    at async loadESM (node:internal/process/esm_loader:91:5)
    at async handleMainPromise (node:internal/modules/run_main:65:12)

Expected Behavior

There shouldn't be any error.

Steps to Reproduce

Just run.

Web3.js Version

v4.0.1-rc.1

Environment

  • Operating System:
  • Browser:
  • Node.js Version:
  • NPM Version:

Anything Else?

No response

@sharifzadesina sharifzadesina added the Bug Addressing a bug label Apr 21, 2023
@sharifzadesina
Copy link
Author

It seems all these errors are because you changed the project from CommonJS to ESM.

@jdevcs jdevcs added the 4.x 4.0 related label Apr 25, 2023
@jdevcs
Copy link
Contributor

jdevcs commented Apr 25, 2023

@sharifzadesina In 4.x RC.1 there is both ESM and CJS builds.
Can you share your sample project with ESM/CJS config that you are using?

@sharifzadesina
Copy link
Author

sharifzadesina commented Apr 26, 2023

Hello @jdevcs,

package.json:

{
  "name": "test",
  "type": "module",
  "dependencies": {
    "web3": "^4.0.1-rc.1"
  },
  "devDependencies": {
    "ts-node": "^10.9.1",
    "tsx": "^3.12.6"
  }
}

tsconfig.json:

{
  "include": ["src/**/*.ts"],
  "exclude": ["node_modules/**"],
  "ts-node": {
    "esm": true,
    "experimentalSpecifierResolution": "node"
  },
  "compilerOptions": {
    "target": "ES2022",

    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,

    "declaration": true,
    "outDir": "dist",
    "inlineSourceMap": true,
    "removeComments": true,

    "module": "ES2022",
    "moduleResolution": "node",
    "rootDir": "src",

    "skipLibCheck": true,

    "strict": true,
    "allowUnreachableCode": false,
    "allowUnusedLabels": false,
    "noFallthroughCasesInSwitch": true,
    "noImplicitOverride": true,
    "noImplicitReturns": true,
    "noPropertyAccessFromIndexSignature": true,
    "noUncheckedIndexedAccess": true
  }
}

src/test.ts file:

import { Web3 } from 'web3';

console.log(Web3);

Runnig using npx tsx --experimental-specifier-resolution=node src/test.ts, result:

import { buf as crc32Buffer } from 'crc-32';
         ^

SyntaxError: The requested module 'crc-32' does not provide an export named 'buf'
    at ModuleJob._instantiate (node:internal/modules/esm/module_job:124:21)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async ModuleJob.run (node:internal/modules/esm/module_job:190:5)

Node.js v18.15.0

Runnig using npx ts-node src/test.ts:

TypeError [ERR_IMPORT_ASSERTION_TYPE_MISSING]: Module "file:///home/sina/Projects/test/node_modules/web3-eth-accounts/lib/esm/common/chains/goerli.json" needs an import assertion of type "json"
    at new NodeError (node:internal/errors:399:5)
    at validateAssertions (node:internal/modules/esm/assert:82:15)
    at defaultLoad (node:internal/modules/esm/load:84:3)
    at nextLoad (node:internal/modules/esm/loader:163:28)
    at /home/sina/Projects/test/node_modules/ts-node/src/esm.ts:255:45
    at async addShortCircuitFlag (/home/sina/Projects/test/node_modules/ts-node/src/esm.ts:409:15)
    at async nextLoad (node:internal/modules/esm/loader:163:22)
    at async ESMLoader.load (node:internal/modules/esm/loader:605:20)
    at async ESMLoader.moduleProvider (node:internal/modules/esm/loader:457:11) {
  code: 'ERR_IMPORT_ASSERTION_TYPE_MISSING'
}

@jdevcs jdevcs self-assigned this Apr 27, 2023
@jdevcs
Copy link
Contributor

jdevcs commented Apr 27, 2023

@sharifzadesina Thanks for testing 4.x RC.1 and sharing feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
4.x 4.0 related Bug Addressing a bug
Projects
None yet
Development

No branches or pull requests

2 participants