question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

React application fails to compile when using a JS library

See original GitHub issue

Current Behavior

A freshly generated react application in a brand new Nx repo will fail to compile when attempting to use a library from the same repo that was generated using the “@nrwl/js” package.

Expected Behavior

I would expect that a js package would work in any application. Library packages generated this way work fine in Node applications.

Steps to Reproduce

  1. Generate a new Nx repo
  2. Generate a new React application with Nx: nx g @nrwl/react:application react-app
  3. Generate a new JS library with Nx: nx g @nrwl/js:library js-lib
  4. Add some exported method with a type signature to the library
  5. Import and use the method of the library in the React application
  6. Run nx serve <react app>
  7. Build fails because it’s trying to load TS files from the lib as plain JS (see Failure Logs section)

Failure Logs

image

Environment

$ nx report

 >  NX   Report complete - copy this into the issue template

   Node : 16.13.2
   OS   : darwin x64
   yarn : 1.22.17

   nx : 13.8.6
   @nrwl/angular : undefined
   @nrwl/cli : 13.8.6
   @nrwl/cypress : 13.8.6
   @nrwl/detox : undefined
   @nrwl/devkit : 13.8.6
   @nrwl/eslint-plugin-nx : 13.8.6
   @nrwl/express : undefined
   @nrwl/jest : 13.8.6
   @nrwl/js : 13.8.6
   @nrwl/linter : 13.8.6
   @nrwl/nest : undefined
   @nrwl/next : undefined
   @nrwl/node : undefined
   @nrwl/nx-cloud : undefined
   @nrwl/react : 13.8.6
   @nrwl/react-native : undefined
   @nrwl/schematics : undefined
   @nrwl/storybook : 13.8.6
   @nrwl/tao : 13.8.6
   @nrwl/web : 13.8.6
   @nrwl/workspace : 13.8.6
   typescript : 4.5.5
   rxjs : 6.6.7
   ---------------------------------------
   Community plugins:

Edit: After comparing the differences between a lib generated with @nrwl/js and one generated with @nrwl/react, it seems the key difference is that the js one doesn’t include a .babelrc file. Is this intentional?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
ajwoottocommented, May 16, 2022
1reaction
ajwoottocommented, Mar 22, 2022

Yeah, the problem is that the JS lib is set up to use “tsc” as its compiler and thus doesn’t include a babelrc, but React applications are compiled with babel and don’t know what to do with those files when the babelrc is missing. You can get a successful production build going by turning off the “buildLibsFromSource” option which will then cause the JS lib to be built separately using its own compiler, but that solution doesn’t help you in dev mode. When serving the React app with the dev server it tries to bundle the whole thing together and ignores the buildLibsFromSource option, so the babelrc is required for dev mode.

I think that’s also an interesting broader question as well, shouldn’t “buildLibsFromSource: false” also affect how running the application in dev mode works?

Read more comments on GitHub >

github_iconTop Results From Across the Web

React Js : Failed to Compile - javascript - Stack Overflow
So whenever i try to run a react project , I get a compile error stating : Failed to compile ./src/index. js Module...
Read more >
Integrating with Other Libraries - React
It can be embedded in other applications and, with a little care, other applications can be embedded in React. This guide will examine...
Read more >
Using React with TypeScript - Mattermost
Learn how to create a new TypeScript app with create-react-app and ... React is a JavaScript library that enables developers to build ......
Read more >
Create React App without Create React App - Bits and Pieces
Step by Step · 1. Make sure node is installed in your system · 2. Create project folder and package.json · 3. Install...
Read more >
A Fresh Create React App But always compiling with Error
So, first thing is to npm install. Then, make sure you're in the right directory and not a parent folder. Finally, npm run...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found