nrwl / nx

Smart Monorepos · Fast CI
https://nx.dev
MIT License
23.65k stars 2.36k forks source link

Npm Packages Are Not Being Resolved in TypeScript Workspaces #8316

Closed NovaMachina closed 2 years ago

NovaMachina commented 2 years ago

Current Behavior

Following the Nx documentation for TypeScript projects I have discovered an issue when importing a npm package into a barebones TypeScript application and then using it in a TS file. It would appear that there is an issue resolving npm packages in TypeScript workspaces.

I did some digging into the stack trace (provided below) and discovered that in @nrwl/js/src/executors/node/node.impl.js that dependencies on line 62 is filled out with the npm package in question, however the outputs array is empty. This then results in the path joining failing on line 65.

dependencies object:

[
  {
    name: 'axios',
    outputs: [],
    node: { type: 'npm', name: 'npm:axios', data: [Object] }
  }
]

Here is the repo that I am using: https://github.com/NovaMachina/payroll-etl

Expected Behavior

Importing npm packages should not break the compilation process and should be able to be used in TypeScript files.

Steps to Reproduce

The following steps were taken:

What is interesting though, is that if you comment out import axios from 'axios'; then run nx serve demoapp, compilation will succeed. Uncommenting import axios from 'axios'; while the Nx process is still running will result in everything working correctly. However, if you then stop the Nx process and restart it with nx serve demoapp, the above error will return.

Failure Logs

nx serve demoapp --verbose nx run demoapp:serve --verbose The "path" argument must be of type string. Received undefined TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received undefined at new NodeError (node:internal/errors:371:5) at validateString (node:internal/validators:119:11) at Object.join (node:path:1172:7) at joinPathFragments (/home/jacob/Projects/TrackNow/react-test/node_modules/@nrwl/devkit/src/utils/path.js:19:31) at /home/jacob/Projects/TrackNow/react-test/node_modules/@nrwl/js/src/executors/node/node.impl.js:64:52 at Array.reduce () at calculateResolveMappings (/home/jacob/Projects/TrackNow/react-test/node_modules/@nrwl/js/src/executors/node/node.impl.js:63:25) at nodeExecutor_1 (/home/jacob/Projects/TrackNow/react-test/node_modules/@nrwl/js/src/executors/node/node.impl.js:37:26) at nodeExecutor_1.next () at resume (/home/jacob/Projects/TrackNow/react-test/node_modules/tslib/tslib.js:225:48)——————————————————————————————————————————————— NX ERROR Running target "demoapp:serve" failed Failed tasks:

  • demoapp:serve Hint: run the command with --verbose for more details.

Environment

Node : 16.13.0 OS : linux x64 npm : 8.3.0

nx : 13.4.1 @nrwl/angular : undefined @nrwl/cli : 13.4.1 @nrwl/cypress : undefined @nrwl/devkit : 13.4.1 @nrwl/eslint-plugin-nx : 13.4.1 @nrwl/express : undefined @nrwl/jest : 13.4.1 @nrwl/linter : 13.4.1 @nrwl/nest : undefined @nrwl/next : undefined @nrwl/node : undefined @nrwl/nx-cloud : undefined @nrwl/react : undefined @nrwl/react-native : undefined @nrwl/schematics : undefined @nrwl/tao : 13.4.1 @nrwl/web : undefined @nrwl/workspace : 13.4.1 @nrwl/storybook : undefined @nrwl/gatsby : undefined typescript : 4.4.4 rxjs : 6.6.7

Community plugins: @nrwl/js: 13.4.1

(node:198277) [DEP0148] DeprecationWarning: Use of deprecated folder mapping "./" in the "exports" field module resolution of the package at /home/jacob/Projects/TrackNow/happynrwl/node_modules/tslib/package.json. Update this package.json to use a subpath pattern like "./*". (Use node --trace-deprecation ... to show where the warning was created)

jackmerrill commented 2 years ago

+1 on this issue, I'm having it too. Tried finding a workaround by creating a workspace library then exporting all from the npm module but that didn't do anything.

Help is needed on this issue!

StickNitro commented 2 years ago

I am experiencing the same problem after adding a nodejs app to my nx workspace, I have applied the fix above locally and can confirm that this resolves the issue for me

github-actions[bot] commented 1 year ago

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.