developit / microbundle

📦 Zero-configuration bundler for tiny modules.
https://npm.im/microbundle
MIT License
8.04k stars 362 forks source link

Error while walking package exports #852

Closed xaviergonz closed 2 years ago

xaviergonz commented 3 years ago

Given the following exports in package.json

  "exports": {
    ".": {
      "import": {
        "webpack": "./dist/index.import.js",
        "default": "./dist/index.import.mjs"
      },
      "require": "./dist/index.js",
      "script": "./dist/index.umd.js",
      "default": "./dist/index.import.mjs"
    }
  },

fails with:

TypeError: Cannot read property '.' of undefined

    at walk (C:\VSProjects\github\mobx-keystone\node_modules\microbundle\dist\cli.js:810:22)
    at walk (C:\VSProjects\github\mobx-keystone\node_modules\microbundle\dist\cli.js:810:10)
    at walk (C:\VSProjects\github\mobx-keystone\node_modules\microbundle\dist\cli.js:810:10)
    at walk (C:\VSProjects\github\mobx-keystone\node_modules\microbundle\dist\cli.js:810:10)
    at getMain (C:\VSProjects\github\mobx-keystone\node_modules\microbundle\dist\cli.js:837:53)
    at createConfig (C:\VSProjects\github\mobx-keystone\node_modules\microbundle\dist\cli.js:927:45)
    at microbundle (C:\VSProjects\github\mobx-keystone\node_modules\microbundle\dist\cli.js:659:18)

Apparently it is first walking inside the ".", then inside "import" and then it fails because "import" is an object without ".", "import" or "module"

It used to work in 0.13.1 but started failing in 0.13.2 and 0.13.3

rschristian commented 3 years ago

Ah, yeah will need the default key in there.

exports was ignored before v0.13.2

make-github-pseudonymous-again commented 3 years ago

Getting the same error with the following config: https://github.com/edit-distance/myers-1986/blob/351a970520f5c1724cc1fdf24d62086ad849f256/package.json#L30-L44

lamualfa commented 3 years ago

Any update from this issue? I can't use the microbundle package because this issue.

whitelizard commented 3 years ago

When using a microbundled package, I get this:

Error [ERR_INVALID_PACKAGE_CONFIG]: Invalid package config .../node_modules/x/package.json. "exports" cannot contain some keys starting with '.' and some not. The exports object must either be an object of package subpath keys or an object of main entry condition name keys only.

rschristian commented 3 years ago

@whitelizard That indicates a misconfigured package, not an issue with Microbundle. Microbundle doesn't generate any export keys for you, so the user needs to write them properly.

Can you share the package?

whitelizard commented 3 years ago

Can you share the package?

Sure, it's this one in this case: https://www.npmjs.com/package/ploson-rule-engine It's not an open repo yet, but npm install it to see its package.json and the built files.

I haven't stripped down the problem, but I think one will get this error when doing import { createRuleEngine } from 'ploson-rule-engine';

rschristian commented 3 years ago

@whitelizard https://www.runpkg.com/?ploson-rule-engine@1.3.2/package.json

Yes, that's invalid and doesn't follow the spec. See: https://nodejs.org/api/packages.html#packages_exports

"." is used as a key when you have multiple subpath exports, i.e., "." -> import ... from 'pkg', "./foo" -> ... from 'pkg/foo', "./bar" -> ... from 'pkg/bar', etc.

In the absence of multiple subpaths, "." has no purpose. Change your package to the following:

"exports": {
    "browser": "./dist/ploson-rule-engine.module.js",
    "umd": "./dist/ploson-rule-engine.umd.js",
    "import": "./dist/ploson-rule-engine.mjs",
    "require": "./dist/ploson-rule-engine.js"
    "default": "./dist/ploson-rule-engine.module.js"
  },

Or whatever keys you want. Point is, you can't define subpaths and conditional exports on the same level, like that warning says. If you're using subpaths, all conditional exports need to be children of subpath keys. See Preact's package.json for a sizable example of this.

make-github-pseudonymous-again commented 3 years ago

In the absence of multiple subpaths, "." has no purpose. Change your package to the following:

@rschristian Do you mean that in my situation:

Getting the same error with the following config: https://github.com/edit-distance/myers-1986/blob/351a970520f5c1724cc1fdf24d62086ad849f256/package.json#L30-L44 https://github.com/developit/microbundle/issues/852#issuecomment-865259708

the exports key of package.json:

rschristian commented 3 years ago

@make-github-pseudonymous-again What I mean is that it does nothing for you.

"exports": {
  ".": {
    "browser": "./dist/index.module.js",
    "umd": "./dist/index.umd.js",
    "require": "./dist/index.cjs",
    "default": "./dist/index.modern.js"
  }
},

should be equivalent to

"exports": {
  "browser": "./dist/index.module.js",
  "umd": "./dist/index.umd.js",
  "require": "./dist/index.cjs",
  "default": "./dist/index.modern.js"
},

Changing this would not solve this issue for you at the moment though, as we expect "import" or "module" keys to exist, see:

https://github.com/developit/microbundle/blob/b1a637486234a2ae784ccf0c512321e2d3efef7c/src/index.js#L262-L265

make-github-pseudonymous-again commented 3 years ago

Changing this would not solve this issue for you at the moment though, as we expect "import" or "module" keys to exist, see:

In the midterm future I will try to add one or both of these keys to see if it fixes the problem.

rschristian commented 3 years ago

In the midterm future I will try to add one or both of these keys to see if it fixes the problem

It definitely would, see the code block I referenced, but there's certainly very valid reasons to not have either of those keys, which was an oversight on my part when writing that. If you can get away with adding one, great! If not, downgrading (for now) might be better.

make-github-pseudonymous-again commented 3 years ago

It definitely would, see the code block I referenced, but there's certainly very valid reasons to not have either of those keys, which was an oversight on my part when writing that. If you can get away with adding one, great! If not, downgrading (for now) might be better.

Yep. Currently running microbundle@0.13.1. I'll reevaluate my position later.

developit commented 3 years ago

I ran into this yesterday, and I believe another solution is to add an explicit import key rather than relying on default:

{
  "exports": {
    ".": {
      "browser": "./dist/index.module.js",
      "umd": "./dist/index.umd.js",
      "require": "./dist/index.cjs",
      "import": "./dist/index.modern.js",  // microbundle will use this instead of failing
      "default": "./dist/index.modern.js"
    }
  }
}