MikeMcl / decimal.js

An arbitrary-precision Decimal type for JavaScript
http://mikemcl.github.io/decimal.js
MIT License
6.35k stars 480 forks source link

Package subpath './decimal' is not defined by "exports" #205

Closed JoePotentier closed 1 year ago

JoePotentier commented 2 years ago

A few days ago I started seeing this error on multiple of our typescript projects (using Nestjs). I think it has something to do with using Namespaces.

I was wondering if this was an issue with our project builds or something related to Decimal.js itself? It's odd that across multiple projects with different setups we were seeing the same error from the same package, which is what brought me here. Any insight would be greatly appreciated.

Thanks.

MikeMcl commented 2 years ago

I added the exports field to the package.json in v10.4.0 published 5 days ago, which means that only the paths explicitly exported can now be imported.

Unfortunately, I do not know much about Nestjs/typescript or why your project is trying to use a non-existent subpath './decimal'.

  "main": "decimal",
  "module": "decimal.mjs",
  "browser": "decimal.js",
  "exports": {
    ".": {
      "types": "./decimal.d.ts",
      "import": "./decimal.mjs",
      "require": "./decimal.js"
    },
    "./decimal.mjs": "./decimal.mjs",
    "./decimal.js": "./decimal.js",
    "./package.json": "./package.json"
  },

Do you get the same error if "main": "decimal", is removed from the package.json?

JoePotentier commented 2 years ago

Downgrading to v10.3.1 fixed it for me. Removing "main": "decimal", had no effect.

JoePotentier commented 2 years ago

We are installing using yarn@1.22.19 for node v16.14.2.

Importing using,

import Decimal from 'decimal.js';

&

import { Decimal } from 'decimal.js';

Changing those from one to another had no effect.

MikeMcl commented 2 years ago

Thanks for letting me know.

I wonder if adding

"./decimal": "./decimal"

to the package.json exports would fix the issue.

I.e.

"exports": {
  ".": {
    "types": "./decimal.d.ts",
    "import": "./decimal.mjs",
    "require": "./decimal.js"
  },
  "./decimal.mjs": "./decimal.mjs",
  "./decimal.js": "./decimal.js",
  "./package.json": "./package.json",
  "./decimal": "./decimal"
},
ReneZeidler commented 1 year ago

I'm facing the same issue.

Adding

"./decimal": "./decimal"

did not work (it results in a new error Cannot find module '/[...]/node_modules/decimal.js/decimal'). However, adding

"./decimal": "./decimal.js"

or

"./decimal": {
  "types": "./decimal.d.ts",
  "import": "./decimal.mjs",
  "require": "./decimal.js"
}

fixes the issue.

MikeMcl commented 1 year ago

@ReneZeidler

Thanks for letting me know. Fixed in v10.4.1.