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

import { BigNumber } gave an Error #300

Closed
emmtte opened this issue Aug 16, 2021 · 2 comments
Closed

import { BigNumber } gave an Error #300

emmtte opened this issue Aug 16, 2021 · 2 comments

Comments

@emmtte
Copy link

emmtte commented Aug 16, 2021

Hi,

With NodeJS 14 or 16

import BigNumber from "bignumber.js";
this work fine but

import { BigNumber } from "bignumber.js";
have this error

import { BigNumber } from "bignumber.js";
         ^^^^^^^^^
SyntaxError: Named export 'BigNumber' not found. The requested module 'bignumber.js' 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 'bignumber.js';
const { BigNumber } = pkg;

    at ModuleJob._instantiate (node:internal/modules/esm/module_job:121:21)
    at async ModuleJob.run (node:internal/modules/esm/module_job:171:5)
    at async Loader.import (node:internal/modules/esm/loader:178:24)
    at async Object.loadESM (node:internal/process/esm_loader:68:5)
    at async handleMainPromise (node:internal/modules/run_main:63:12)
@MikeMcl
Copy link
Owner

MikeMcl commented Aug 16, 2021

Yes, if you are not using a bundler such as webpack or rollup which uses the module field of the package.json, then you would need to import using the path to the module file

import { BigNumber } from "./node_modules/bignumber.js/bignumber.mjs";

Notes:

I am not absolutely sure about this, but in earlier versions of Node (<13?),

import { BigNumber } from "bignumber.js";

would work, because the "main" field in the package.json here is extensionless, and the old import path resolution algorithm looked for a file with the ".mjs" extension first. See 4.4. Shipping both ESM and CJS and Dual-Mode Packages.

That is no longer the case, so now

import BigNumber from "bignumber.js";

will actually load the bignumber.js file rather than the bignumber.mjs file, and only the default export is available when importing from CommonJs files, so named exports such as in

import { BigNumber } from "bignumber.js";

no longer work.

The new import resolver algorithm is here.

The solution is to add the "exports" field to the package.json

"exports": {
  "import": "./bignumber.mjs",
  "require": "./bignumber.js"
}

but that will prevent importing from subpaths of the package, for example

import { BigNumber } from "./node_modules/bignumber.js/bignumber.mjs";

so it likely to cause some users problems initially.

I also need to consider typescript, webpack, rollup, angular etc.

Related:
Modules: Packages
Hybrid npm packages (ESM and CommonJS)
ES Module not used in preference to CommonJS module for big.js
big.js is a CommonJS dependency
#217 #278

@emmtte
Copy link
Author

emmtte commented Aug 17, 2021

OK, thanks for your help

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

No branches or pull requests

2 participants