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

Nx NextJS can't import js lib #15785

Closed
tiagobnobrega opened this issue Mar 20, 2023 · 5 comments · Fixed by #15795
Closed

Nx NextJS can't import js lib #15785

tiagobnobrega opened this issue Mar 20, 2023 · 5 comments · Fixed by #15795
Assignees
Labels
outdated scope: nextjs Issues related to NextJS support for Nx type: bug

Comments

@tiagobnobrega
Copy link

tiagobnobrega commented Mar 20, 2023

Current Behavior

When importing from a lib created using npx nx g @nrwl/js:lib mylib-wp the module is not be resolved by next.

I'm getting the following output from nx serve web-app:

error - ./pages/_app.tsx:5:0
Module not found: Can't resolve '@nextjs-test/mylib-wp'
  3 | import './styles.css';
  4 | // import { mylib } from '@nextjs-test/mylib';
> 5 | import {mylibWp} from '@nextjs-test/mylib-wp'
  6 |
  7 | function CustomApp({ Component, pageProps }: AppProps) {
  8 |   console.log('My lib:' + mylibWp());

I also added a nextjs lib using npx nx g @nrwl/next:lib mylib-next, and this one works.

Expected Behavior

Libraries created using the js generator should be resolved by next.

GitHub Repo

tiagobnobrega/nx-nextjs-1590b2

Steps to Reproduce

  1. call nx serve web-app
  2. Observe output

Nx Report

➜  nextjs-test git:(main) nx report
>  NX  Falling back to ts-node for local typescript execution. This may be a little slower.
  - To fix this, ensure @swc-node/register and @swc/core have been installed

  NX   Report complete - copy this into the issue template

   Node : 16.14.0
   OS   : darwin arm64
   npm  : 8.3.1

   nx                      : 15.9.0-beta.6
   @nrwl/js                : 15.9.0-beta.6
   @nrwl/jest              : 15.9.0-beta.6
   @nrwl/linter            : 15.9.0-beta.6
   @nrwl/workspace         : 15.9.0-beta.6
   @nrwl/cli               : 15.9.0-beta.6
   @nrwl/cypress           : 15.9.0-beta.6
   @nrwl/devkit            : 15.9.0-beta.6
   @nrwl/eslint-plugin-nx  : 15.9.0-beta.6
   @nrwl/next              : 15.9.0-beta.6
   @nrwl/react             : 15.9.0-beta.6
   @nrwl/tao               : 15.9.0-beta.6
   @nrwl/vite              : 15.9.0-beta.6
   @nrwl/webpack           : 15.9.0-beta.6
   typescript              : 4.9.5

Failure Logs

error - ./pages/_app.tsx:6:0
Module not found: Can't resolve '@nextjs-test/mylib'
  4 | // import {mylibWp} from '@nextjs-test/mylib-wp'
  5 | import {MylibNext, mylibNextTs} from "@nextjs-test/mylib-next";
> 6 | import { mylib } from '@nextjs-test/mylib';
  7 |
  8 | function CustomApp({ Component, pageProps }: AppProps) {
  9 |   console.log('My lib:' + mylibNextTs());

https://nextjs.org/docs/messages/module-not-found
[ ready ] on http://localhost:4200

Additional Information

Was originally testing this in 15.9.0-beta.2 but then switched to 15.9.0-beta.6. Same issue.

No response

@tiagobnobrega tiagobnobrega changed the title Nx Nextj can't import js lib Nx NextJS can't import js lib Mar 20, 2023
@StringKe
Copy link

StringKe commented Mar 21, 2023

+1, I encountered the same error and could not import.

>  NX  Falling back to ts-node for local typescript execution. This may be a little slower.
  - To fix this, ensure @swc-node/register and @swc/core have been installed

 >  NX   Report complete - copy this into the issue template

   Node : 18.15.0
   OS   : darwin arm64
   pnpm : 7.30.0
   
   nx                      : 15.8.7
   @nrwl/js                : 15.8.7
   @nrwl/jest              : 15.8.7
   @nrwl/linter            : 15.8.7
   @nrwl/workspace         : 15.8.7
   @nrwl/cli               : 15.8.7
   @nrwl/cypress           : 15.8.7
   @nrwl/devkit            : 15.8.7
   @nrwl/esbuild           : 15.8.7
   @nrwl/eslint-plugin-nx  : 15.8.7
   @nrwl/nest              : 15.8.7
   @nrwl/next              : 15.8.7
   @nrwl/node              : 15.8.7
   @nrwl/nx-plugin         : 15.8.7
   @nrwl/react             : 15.8.7
   @nrwl/rollup            : 15.8.7
   @nrwl/tao               : 15.8.7
   @nrwl/vite              : 15.8.7
   @nrwl/web               : 15.8.7
   @nrwl/webpack           : 15.8.7
   @nrwl/nx-cloud          : 15.2.3
   typescript              : 4.9.5
   ---------------------------------------
   Community plugins:
   @jscutlery/semver : 2.30.1

15.8.6 is not error

@jaysoo
Copy link
Member

jaysoo commented Mar 21, 2023

@tiagobnobrega I don't see any apps or libs in the repo. Maybe they weren't added to git?

I'm able to import workspace libs generated by @nrwl/js:lib as well.

https://github.com/jaysoo/next-workspace-lib/blob/main/apps/demo/pages/_app.tsx#L8

Edit: I didn't see the packages folder. I'll double-check.

@jaysoo jaysoo self-assigned this Mar 21, 2023
@jaysoo jaysoo added the scope: nextjs Issues related to NextJS support for Nx label Mar 21, 2023
@jaysoo
Copy link
Member

jaysoo commented Mar 21, 2023

This will be fixed in 15.9, the workaround for now is to set buildLibsFromSource: false in the app's project.json file.

@tiagobnobrega
Copy link
Author

This will be fixed in 15.9, the workaround for now is to set buildLibsFromSource: false in the app's project.json file.

Thanks so much. I'll try this out

@github-actions
Copy link

This issue has been closed for more than 30 days. If this issue is still occuring, please open a new issue with more recent context.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 21, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated scope: nextjs Issues related to NextJS support for Nx type: bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants