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.

nx build produce only types, not executable .js code

See original GitHub issue

I want to use nx as a place, that contain publishable react-native libraries, which I can easily develop and distribute.

For achieve this, I do all steps describes in tutorial page

Steps to Reproduce

  1. Create workspace without nx cloud
npx create-nx-workspace your-workspace-name \
--preset=react-native \
--appName=your-app-name
  1. cd your-workspace-name

  2. Generate publishable react-native library

npx nx g @nrwl/react-native:lib your-lib-name --publishable --importPath @whalemare/your-lib-name
  1. Add some stub files with logic (interface Logger, and implementation ConsoleLogger)

  2. Prepare lib for publishing, by calling npx nx build your-lib-name

  3. Try to execute bundled code with node index.umd.js

Current Behavior

NX build with success only entry point of library without any implementation details

I was try to dig deeper, and found that build is done with rollup. But I don’t know why it emit only typings, without implementation details.

Bundling your-lib-name...
  index.esm.js 57 Bytes
  index.umd.js 195 Bytes
⚡ Done in 1.18s
Снимок экрана 2022-03-09 в 18 09 45

When I execute bundled code with node index.umd.js I receive ‘MODULE_NOT_FOUND’ error

node index.umd.js 
node:internal/modules/cjs/loader:936
  throw err;
  ^

Error: Cannot find module './logger'
Require stack:
- /Users/whalemare/dev/temp/your-workspace-name/dist/libs/your-lib-name/index.umd.js
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
    at Function.Module._load (node:internal/modules/cjs/loader:778:27)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at /Users/whalemare/dev/temp/your-workspace-name/dist/libs/your-lib-name/index.umd.js:6:186
    at /Users/whalemare/dev/temp/your-workspace-name/dist/libs/your-lib-name/index.umd.js:3:2
    at Object.<anonymous> (/Users/whalemare/dev/temp/your-workspace-name/dist/libs/your-lib-name/index.umd.js:4:3)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/Users/whalemare/dev/temp/your-workspace-name/dist/libs/your-lib-name/index.umd.js'
  ]
}

Expected Behavior

NX build entry points and implementations files, how it do tsc compiler. dist/libs/your-lib-name folder contains as index.js file and ConsoleLogger.js

Снимок экрана 2022-03-09 в 18 32 13

Repository with reproducible example: https://github.com/whalemare/nx-reproduce-not-found-module.git

Environment

>  NX   Report complete - copy this into the issue template

   Node : 16.13.1
   OS   : darwin arm64
   npm  : 8.1.2
   
   nx : 13.8.5
   @nrwl/angular : undefined
   @nrwl/cli : 13.8.5
   @nrwl/cypress : 13.8.5
   @nrwl/detox : 13.8.5
   @nrwl/devkit : 13.8.5
   @nrwl/eslint-plugin-nx : 13.8.5
   @nrwl/express : undefined
   @nrwl/jest : 13.8.5
   @nrwl/js : 13.8.5
   @nrwl/linter : 13.8.5
   @nrwl/nest : undefined
   @nrwl/next : undefined
   @nrwl/node : undefined
   @nrwl/nx-cloud : undefined
   @nrwl/react : 13.8.5
   @nrwl/react-native : 13.8.5
   @nrwl/schematics : undefined
   @nrwl/storybook : 13.8.5
   @nrwl/tao : 13.8.5
   @nrwl/web : 13.8.5
   @nrwl/workspace : 13.8.5
   typescript : 4.5.5
   rxjs : 6.6.7
   ---------------------------------------
   Community plugins:

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
xiongemicommented, Jul 30, 2022

to build the react native library, there are a few steps:

  1. inside your library’s .babelrc, change it to:
{
  "presets": [
    [
      "@nrwl/react/babel",
      {
        "runtime": "automatic",
        "useBuiltIns": "usage"
      }
    ]
  ]
}

2 in your lib’s tsconfig.lib.json, add below to compilerOptions:

"jsx": "react-native"
  1. in your lib’s project.json, for targets -> build -> external, make sure react-native is in the array:
"external": ["react/jsx-runtime", "react-native"],
0reactions
nicost71commented, Sep 13, 2022

I managed to get the (compiled) source-code in the dist/index.js by exporting the js-files in the original index.js using “export * from ./mycomponent” etc.

it’s right though, that the components itself are only visible as type-definition (e.g. mycomponent.d.ts) and it’s (compiled) code is all in the index.js, right?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Node Tutorial - Part 1: Code Generation - Nx
In this tutorial you'll create a backend-focused workspace with Nx. ... To create the products-data-client library, use the @nrwl/js:lib generator: Terminal.
Read more >
Nrwl Nx build node project only transpiles main.ts
ts exists. When I run nx build [project-name] , only main.ts file gets transpiled into a .js file in the out directory.
Read more >
Pnpm and Nx monorepo. Part 2 - Javier Brea
Let's create our first project in the packages folder. We will name it sum-one . It will contain its own package.json file, and...
Read more >
Integrating GraphQL Code Generator in your frontend ...
GraphQL Code Generator can generate more than just TypeScript types. It can automate some of your GraphQL development workflow, ...
Read more >
Node JS Tutorial: The Basics
Prior to Node.js, JavaScripts runs only under a web browser (such as Chrome, ... Instead of adding the bin directory to the PATH,...
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