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

Wrong exports in tslib #161

Open
jogibear9988 opened this issue Nov 20, 2021 · 9 comments
Open

Wrong exports in tslib #161

jogibear9988 opened this issue Nov 20, 2021 · 9 comments

Comments

@jogibear9988
Copy link

I think the "import" specifier in the exports section should also point to ES6 Version:

"import": "./modules/index.js",

see spec:
https://nodejs.org/api/packages.html#approach-1-use-an-es-module-wrapper

  Node.js implements the following conditions:
  
  "import" - matches when the package is loaded via import or import(), or via any top-level import or resolve operation by the ECMAScript module loader. Applies regardless of the module format of the target file. Always mutually exclusive with "require".
  "require" - matches when the package is loaded via require(). The referenced file should be loadable with require() although the condition matches regardless of the module format of the target file. Expected formats include CommonJS, JSON, and native addons but not ES modules as require() doesn't support them. Always mutually exclusive with "import".
  "node" - matches for any Node.js environment. Can be a CommonJS or ES module file. This condition should always come after "import" or "require".
  "node-addons" - similar to "node" and matches for any Node.js environment. This condition can be used to provide an entry point which uses native C++ addons as opposed to an entry point which is more universal and doesn't rely on native addons. This condition can be disabled via the --no-addons flag.
  "default" - the generic fallback that always matches. Can be a CommonJS or ES module file. This condition should always come last.

so there is no "module" specifier, but it is often used, I know. But if I want to import acording to spec, my site would load the one with "import" and then it would fail cause this is not usable by imports

@jogibear9988
Copy link
Author

fix #162

@rbuckton
Copy link
Member

rbuckton commented Feb 8, 2022

tslib.es6.js predates NodeJS's support for ES Modules. If you change the "import" directive in the export map as you've proposed, NodeJS will attempt to load the file as a CommonJS module, resulting in the following error:

SyntaxError Unexpected token 'export'

The tslib.es6.js file can be used in the browser, but not in NodeJS.

We currently use "import": "./modules/index.js", as that file is in a directory with a package.json that includes { "type": "module" } so that NodeJS will treat it as an ES Module. That file simply re-exports the CommonJS tslib.js module.

From our tests, this is working as expected. What is the actual issue you are running into?

@jogibear9988
Copy link
Author

My problem is, I don't use a package manager, I host directly the javascript emited from typescript, and this points to "./node_modules/tslib". Now my webserver follows the resolution strategy and rewrites the import to tslib.js, cause there is no definition of "module" for the export in the spec of nodejs

@jogibear9988
Copy link
Author

Maybe switching the package.json to type"module" would fix this? so the export with "main" and "require" should point to a comonJS module then, and the rest to es6?

@rbuckton
Copy link
Member

rbuckton commented Feb 8, 2022

We can't change the root package.json in tslib to "type": "module" without breaking tslib.js. That's the reason we have a modules folder that has its own package.json.

@rbuckton
Copy link
Member

rbuckton commented Feb 8, 2022

Will #171 address your needs?

@jogibear9988
Copy link
Author

I think this would work.

@jogibear9988
Copy link
Author

thx

@justinfagnani
Copy link

Is there hope of getting this resolved even thought #171 has a disapproval?

As far as I know, "module" isn't one of the standard export conditions, and "import" is the condition that should point to a standard JS module.

We're hitting issues where we load tslib as a standard module, and get the error:

The requested module '../tslib' does not provide an export named 'default'

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

3 participants