-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
[Bug]: This module cannot be imported from a Client Component module. It should only be used from a Server Component. #13967
Comments
+1 |
I was able to work around this with just mocking
|
Hitting this too. mocking server-only didn't work either |
Could someone provide minimal reproduction repo? |
This is almost minimal: TxHawks/deadliner. There's a single module with tests there ( If this isn't minimal enough, let me know and I'll make something even more minimal |
Thanks. Look at the "exports": {
".": {
"react-server": "./empty.js",
"default": "./index.js"
}
}
To point Jest resolver to the /**
* @jest-environment node
* @jest-environment-options {"customExportConditions": ["react-server"]}
*/ The test will pass. (There is some error from Next.js, but that is not Jest specific.) Unfortunately I can’t find any usage documentation of the |
Thanks! As far as I know, |
UPDATE This can't work on React side. See: reactjs/rfcs#227 (comment) |
The I still get the same error even with: /**
* @jest-environment node
* @jest-environment-options {"customExportConditions": ["react-server"]}
*/ Or putting this in jest.config.js: testEnvironmentOptions: {
customExportConditions: ['react-server', 'node', 'node-addons'],
}, |
Try removing all Jest config magic coming from module.exports = {}; Run the test without and with the suggested pragma. In first case you will see the error, adding the pragma fixes it. All is solved here.
|
Yeah, I'm not really sure how to do that in a Next project. Have a different config for different files in the project |
Seems like this is a bug in Next.js. See vercel/next.js#47448 By the way, using the pragma and |
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days. |
This issue was closed because it has been stalled for 30 days with no activity. Please open a new issue if the issue is still relevant, linking to this one. |
This issue was closed because it has been stalled for 30 days with no activity. Please open a new issue if the issue is still relevant, linking to this one. |
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Version
29.4.3
Steps to reproduce
Create any component and add the
import "server-only"
(https://www.npmjs.com/package/server-only) to mark it as server only code.Try to run any test against it and they are going to fail and log the following message:
This module cannot be imported from a Client Component module. It should only be used from a Server Component.
Expected behavior
The test(s) should be able to run without any issues.
Actual behavior
Test fail and log
This module cannot be imported from a Client Component module. It should only be used from a Server Component.
Additional context
I've already tried the following:
testEnvironment: 'node'
Environment
The text was updated successfully, but these errors were encountered: