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

Undefined function when importing float16 from another library in CRA which uses module bundler #900

Closed
1038642892 opened this issue Mar 8, 2023 · 6 comments
Labels

Comments

@1038642892
Copy link

Scenario:
My library contains your package as a dependency, this library is imported from create-react-app (CRA) project.

Library code:

const { getFloat16 } = require("@petamoriken/float16");

function conversion(value) {
  var buffer = new ArrayBuffer(4);
  var view = new DataView(buffer);
  view.setUint16(0, value);
  return getFloat16(view, 0, false);
}

The issue:

Uncaught TypeError: getFloat16 is not a function
    at myLibrary(conversion.js:7:1)

Why would the import inside the CRA project be undefined when the library has it as dependency?

I've checked for circular dependencies using:
npx madge --circular --extensions ts,js ./

@1038642892
Copy link
Author

To verify other libraries work i've also required lodash:

const array = require('lodash/array');

and simply print within the conversion function at beginning:

console.log(array.chunk(['a', 'b', 'c', 'd'], 2));

No issues there.

Could be some mismatch in the way float16 is exported?

@petamoriken
Copy link
Owner

I have tried it with the latest CRA and have confirmed this issue.

It appears that the CommonJS style with require has a problem with the bundler and returns the location of the file.

const f = require("@petamoriken/float16");
console.log(f); // => "/static/media/index.9ea9595cf22b971e0a2e.cjs"

On the other hand, the ES Module style seems to work fine.

import { getFloat16 } from '@petamoriken/float16';

function conversion(value) {
  var buffer = new ArrayBuffer(4);
  var view = new DataView(buffer);
  view.setUint16(0, value);
  return getFloat16(view, 0, false);
}

@1038642892
Copy link
Author

Potentially an issue with CRA Add support for CJS files #12700

@1038642892
Copy link
Author

Or webpack in general since main and module is defined in package.json webpack/webpack#5756 (comment)

@petamoriken
Copy link
Owner

Probably a CJS issue for the CRA.

"main": "./lib/index.cjs",

@1038642892
Copy link
Author

I can confirm that - I've just posted a comment on the CRA issue - this is how i've worked around it for now: facebook/create-react-app#12700 (comment)

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

No branches or pull requests

2 participants