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

Can't import the named export 'RestError' from non EcmaScript module (only default export is available) #22172

Closed
subhod-i opened this issue Jun 9, 2022 · 15 comments
Assignees
Labels
Azure.Core Client This issue points to a problem in the data-plane of the library. customer-reported Issues that are reported by GitHub users external to the Azure organization. question The issue doesn't require a change to the product in order to be resolved. Most issues start as that

Comments

@subhod-i
Copy link

subhod-i commented Jun 9, 2022

  • Package Name: @azure/core-rest-pipeline
  • Package Version: 1.8.1
  • Operating system: MacOS Mont.
  • create-react-app: : 4.0.0

Describe the bug
Trying to perform
import { RestError } from '@azure/core-rest-pipeline';
in a ECMAScript Module file (.mjs).

returns build error with the message
Can't import the named export 'RestError' from non EcmaScript module (only default export is available)

To Reproduce
Steps to reproduce the behavior:
I have created a simple project to demonstrate this

  1. git clone git@github.com:subhod-i/react-app-4.git
  2. cd react-app-4
  3. npm i
  4. npm start

Expected behavior
Support for ECMA module files.

Additional context
The same problem is observed in @azure/core-client package too.

@ghost ghost added needs-triage Workflow: This is a new issue that needs to be triaged to the appropriate team. customer-reported Issues that are reported by GitHub users external to the Azure organization. question The issue doesn't require a change to the product in order to be resolved. Most issues start as that labels Jun 9, 2022
@azure-sdk azure-sdk added Azure.Core Client This issue points to a problem in the data-plane of the library. needs-team-triage Workflow: This issue needs the team to triage. labels Jun 9, 2022
@ghost ghost removed the needs-triage Workflow: This is a new issue that needs to be triaged to the appropriate team. label Jun 9, 2022
@jeremymeng
Copy link
Member

The conditional export feature may be a solution.

We need to be careful about state though

@xirzec
Copy link
Member

xirzec commented Jun 9, 2022

Let me make sure I understand the issue here:

  • We only expose our MJS version by using the module field in package.json, which is not used by Node, but is used by bundlers.
  • Since Node can't find our MJS version, it loads our CJS version which doesn't play nicely with import statements.

The request then is to expose our MJS version to Node's native ES Module support. In order to do that, we must use conditional imports and either:

  1. Rename our CJS files to .cjs and set "type": "module"
  2. Rename our MJS files to .mjs

If we go with 1, we have to validate that older Node runtimes understand how to load .cjs files.
If we pick 2, we have to make sure that bundlers are still able to pull in the .mjs files and we don't break TS typings.

Am I missing anything?

@xirzec
Copy link
Member

xirzec commented Jun 9, 2022

It seems like .cjs was added in Node 12, so we should be okay to use it.

@joheredi joheredi removed the needs-team-triage Workflow: This issue needs the team to triage. label Jul 2, 2022
@subhod-i
Copy link
Author

@jeremymeng Any updates on this?

@xirzec
Copy link
Member

xirzec commented Jul 29, 2022

@mpodwysocki this is related to the investigation you are doing for native Node module support

@mpodwysocki
Copy link
Member

@xirzec correct, we could fix this with the exports and ESM modules

@jeremymeng
Copy link
Member

@subhod-i I tested the scenario and PR #22804 appears to have addressed the issue. Will continue working on getting it merged.

@jeremymeng
Copy link
Member

@subhod-i also it looks like an issue in create-react-app v4. The latest v5 worked fine for me without changes from PR #22804

@subhod-i
Copy link
Author

subhod-i commented Oct 18, 2022

@subhod-i also it looks like an issue in create-react-app v4. The latest v5 worked fine for me without changes from PR #22804

React native apps still use v4 and that is why we had to support the v4 version too. @jeremymeng Thank you for sharing the StackOverflow links. @davidyuk we can see these solution work or wait till the #22804 gets merged into master.

@jeremymeng
Copy link
Member

I did some testing with @subhod-i's repro project. With PR #22804, webpack 4 now reports a different error:

Attempted import error: 'RestError' is not exported from '@azure/core-rest-pipeline'.

Importing from a .mjs file in a simple project works fine when webpack is not involved. It feels that webpack doesn't work very well with ESM. I will continue to check if there's anything we can do.

@jeremymeng
Copy link
Member

It is still not clear to me why from a .mjs file, webpack couldn't use the module field in our package.json. However, the following workaround seems to work for me

import crp from '@azure/core-rest-pipeline';
console.log(crp.RestError);

@davidyuk
Copy link

However, the following workaround seems to work for me

This would work in create-react-app@4, but won't work in create-react-app@5 as I know. The thing is that we are library that depends on an api generated by autorest and would like to be compatible with both create-react-app@4 and create-react-app@5, I don't see a way to import @azure/core-rest-pipeline in a way compatible with both.

It feels that webpack doesn't work very well with ESM. I will continue to check if there's anything we can do.

webpack@4 has a quirk, but it is doable to fix this on the side of your package, e.g. using mjs extension or adding "type": "module" to package.json

React native apps still use v4

I'm not sure about this, probably they are using their own builder, so, the issue is only aboue compatibility with create-react-app@4

we can see these solution work

As I know, we can't add javascript/auto with no ejecting

@jeremymeng
Copy link
Member

webpack@4 has a quirk, but it is doable to fix this on the side of your package, e.g. using mjs extension or adding "type": "module" to package.json

@davidyuk I added "type": "module" in PR #22804. However, the repro project now gives another error as mentioned above. I am still trying to figure out whether there's anything I can do to fix that.

jeremymeng added a commit that referenced this issue Oct 20, 2022
- Add conditional exports for core-rest-pipeline
- Add .js extension to imported source module name
- Bump dev dependency Mocha version to v10
- Add `ts-node/esm` loader in mocha config
- Update npm scripts
- Update linter rule to allow dist/index.cjs
- Add a copy of our typing files for cjs
- Bump minor version

### Packages impacted by this PR
`@azure/core-rest-pipeline`

### Issues associated with this PR
#22172
mpodwysocki pushed a commit that referenced this issue Nov 2, 2022
- Add conditional exports for core-rest-pipeline
- Add .js extension to imported source module name
- Bump dev dependency Mocha version to v10
- Add `ts-node/esm` loader in mocha config
- Update npm scripts
- Update linter rule to allow dist/index.cjs
- Add a copy of our typing files for cjs
- Bump minor version

### Packages impacted by this PR
`@azure/core-rest-pipeline`

### Issues associated with this PR
#22172
Copy link

Hi @subhod-i, we deeply appreciate your input into this project. Regrettably, this issue has remained unresolved for over 2 years and inactive for 30 days, leading us to the decision to close it. We've implemented this policy to maintain the relevance of our issue queue and facilitate easier navigation for new contributors. If you still believe this topic requires attention, please feel free to create a new issue, referencing this one. Thank you for your understanding and ongoing support.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Jun 10, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Jun 10, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Azure.Core Client This issue points to a problem in the data-plane of the library. customer-reported Issues that are reported by GitHub users external to the Azure organization. question The issue doesn't require a change to the product in order to be resolved. Most issues start as that
Projects
None yet
Development

No branches or pull requests

7 participants