nx: [@nwrl/js] TypeError [ERR_INVALID_ARG_TYPE]: The "to" argument must be of type string. Received undefined

Current Behavior

A specific package with one dependency always fail even the build from itself and the dependency are successful.

Expected Behavior

Don’t fail and stop the run.

Github Repo

No response

Steps to Reproduce

  1. After realize my code was actually fine and completely built, I went verbose mode
  2. I found an error being thrown at /node_modules/@nrwl/js/src/utils/inline.js:171:48
  3. This line is the built version of this source code, which naively assume that result is always a valid key of inlinedDepsDestOutputRecord, and it’s value is a string
        const importPath = `"${relative(
          dirPath,
          inlinedDepsDestOutputRecord[result]
        )}"`;
  1. I put a console.log, which it’s output can be found below, and confirmed that result is actually undefined

I don’t know how to reproduce it without exposing private code, I didn’t found anything special different from any other file or package in this project that could explain such behavior.

Since I don’t quite understand the core of this tool, so I can’t explain it.

I’m sincerely sorry, but I’ve already lost many many hours trying to understand what was wrong with my code (that works and build perfectly fine outside nx workspace) to spend many more trying to figure how to reproduce it without changing the tool itself to test.

Nx Report

>  NX   Report complete - copy this into the issue template

   Node : 16.17.1
   OS   : darwin x64
   npm  : 8.19.2
   
   nx : 15.4.2
   @nrwl/angular : Not Found
   @nrwl/cypress : 15.4.2
   @nrwl/detox : Not Found
   @nrwl/devkit : 15.4.2
   @nrwl/esbuild : Not Found
   @nrwl/eslint-plugin-nx : 15.4.2
   @nrwl/expo : Not Found
   @nrwl/express : Not Found
   @nrwl/jest : 15.4.2
   @nrwl/js : 15.4.2
   @nrwl/linter : 15.4.2
   @nrwl/nest : 15.4.2
   @nrwl/next : Not Found
   @nrwl/node : 15.4.2
   @nrwl/nx-cloud : 15.0.2
   @nrwl/nx-plugin : 15.4.2
   @nrwl/react : Not Found
   @nrwl/react-native : Not Found
   @nrwl/rollup : 15.4.2
   @nrwl/schematics : Not Found
   @nrwl/storybook : Not Found
   @nrwl/web : 15.4.2
   @nrwl/webpack : 15.4.2
   @nrwl/workspace : 15.4.2
   @nrwl/vite : Not Found
   typescript : 4.9.4
   ---------------------------------------
   Local workspace plugins:
   ---------------------------------------
   Community plugins:
   	 @wanews/nx-esbuild: 0.26.1
   	 @wanews/nx-typescript-project-references: 0.21.0

Failure Logs

nx run utils:build

Compiling TypeScript files for project "utils"...
Done compiling TypeScript files for project "utils".
{
  dirPath: '[project_root_dir]/dist/packages/libs/utils/src',
  importRegex: /(?:)/g,
  inlinedDepsDestOutputRecord: {},
  filePath: '[project_root_dir]/dist/packages/libs/utils/src/address.d.ts',
  matched: '',
  result: '',
  value: undefined
}
node:internal/errors:477
    ErrorCaptureStackTrace(err);
    ^

TypeError [ERR_INVALID_ARG_TYPE]: The "to" argument must be of type string. Received undefined
    at new NodeError (node:internal/errors:387:5)
    at validateString (node:internal/validators:121:11)
    at relative (node:path:1192:5)
    at [project_root_dir]/node_modules/@nrwl/js/src/utils/inline.js:171:48
    at String.replace (<anonymous>)
    at recursiveUpdateImport ([project_root_dir]/node_modules/@nrwl/js/src/utils/inline.js:168:48)
    at recursiveUpdateImport ([project_root_dir]/node_modules/@nrwl/js/src/utils/inline.js:177:13)
    at updateImports ([project_root_dir]/node_modules/@nrwl/js/src/utils/inline.js:158:5)
    at postProcessInlinedDependencies ([project_root_dir]/node_modules/@nrwl/js/src/utils/inline.js:52:5)
    at [project_root_dir]/node_modules/@nrwl/js/src/executors/tsc/tsc.impl.js:87:57 {
  code: 'ERR_INVALID_ARG_TYPE'
}

Additional Information

No response

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 5
  • Comments: 35 (6 by maintainers)

Most upvoted comments

Yet not stale

True

This issue has been automatically marked as stale because it hasn’t had any recent activity. It will be closed in 14 days if no further activity occurs. If we missed this issue please reply to keep it active. Thanks for being a part of the Nx community! 🙏

Again, this is not stale. The problem still remains with no official solution for it.

This issue has been automatically marked as stale because it hasn’t had any recent activity. It will be closed in 14 days if no further activity occurs. If we missed this issue please reply to keep it active. Thanks for being a part of the Nx community! 🙏

@nartc, there is any test I can make to help solving this issue?

As I mentioned before, I don’t have spare time to learn about nx’s codebase, but I want to collaborate with the project.


If, by any chance, someone need an immediate workaround, it is nasty, but adding the following line on the mentioned file works for this version…

node_modules/@nrwl/js/src/utils/inline.js:170

if (!(result in inlinedDepsDestOutputRecord)) return result

I’m not quite sure if this have side effects, so, I would recommend against doing it on production environments…

This issue has been automatically marked as stale because it hasn’t had any recent activity. It will be closed in 14 days if no further activity occurs. If we missed this issue please reply to keep it active. Thanks for being a part of the Nx community! 🙏

Yet not stale

@SkyaTura Okay so I found the issue that I was facing. All of my non publishable libraries were buildable (I made them buildable because of the eslint rule).

This caused

const shouldInline =
        /**
         * if all buildable libraries are marked as external,
         * then push the project dependency that doesn't have a build target
         */
        (options.external === 'all' && !buildOutputPath) ||
            /**
             * if all buildable libraries are marked as internal,
             * then push every project dependency to be inlined
             */
            options.external === 'none' ||
            /**
             * if some buildable libraries are marked as external,
             * then push the project dependency that IS NOT marked as external OR doesn't have a build target
             */
            (Array.isArray(options.external) &&
                options.external.length > 0 &&
                !options.external.includes(projectDependency.target)) ||
            !buildOutputPath;

to be false. If shouldInline is false, it does not set the inlinedDepsDestOutputRecord thus this always being empty. This might be the cause of your problem.

After I removed all the builds for the non publishable libraries. I faced a new issue: Error: ENOENT: no such file or directory, lstat '/Users/itrulia/Documents/wepublish/dist/libs/website/libs/website-api'. This error happens because of this line: movePackage(depOutputPath, destDepOutputPath);

When I console.log the variables, I got:

/Users/itrulia/Documents/wepublish/dist/libs/website libs/page/website
/Users/itrulia/Documents/wepublish/dist/libs/website libs/article/website
/Users/itrulia/Documents/wepublish/dist/libs/website libs/navigation/website
/Users/itrulia/Documents/wepublish/dist/libs/website libs/richtext/website
/Users/itrulia/Documents/wepublish/dist/libs/website libs/membership/website
/Users/itrulia/Documents/wepublish/dist/libs/website libs/block-content/website
/Users/itrulia/Documents/wepublish/dist/libs/website libs/authentication/website
/Users/itrulia/Documents/wepublish/dist/libs/website libs/website-builder
/Users/itrulia/Documents/wepublish/dist/libs/website libs/website-api
/Users/itrulia/Documents/wepublish/dist/libs/website libs/website-api-v2
/Users/itrulia/Documents/wepublish/dist/libs/website libs/website-api

/Users/itrulia/Documents/wepublish/dist/libs/website libs/website-api was in the output twice, the 2nd time this is logged, the build fails.

If I console.log Object.values(inlineGraph.dependencies), I am getting this:

[
  [
    'page-website',
    'article-website',
    'navigation-website',
    'richtext-website',
    'membership-website',
    'block-content-website',
    'authentication-website',
    'website-builder',
    'website-api',
    'website-api-v2'
  ],
  [ 'website-api', 'website-builder', 'block-content-website' ],
  [
    'website-api', 'website-api',
    'website-api', 'website-api',
    'website-api', 'website-api',
    'website-api', 'website-api',
    'website-api', 'website-api',
    'website-api', 'website-api'
  ],
  [
    'website-builder',
    'website-api',
    'richtext-website',
    'website-builder',
    'website-api',
    'richtext-website',
    'website-builder',
    'website-api',
    'richtext-website'
  ],
  [
    'website-builder',
    'website-builder',
    'website-builder',
    'website-builder'
  ],
  [ 'website-api', 'website-builder', 'block-content-website' ],
  [ 'website-api', 'website-builder' ],
  [ 'website-builder', 'website-api', 'authentication-website' ],
  [ 'website-api', 'website-api' ]
]

This is the cause.

re https://github.com/nrwl/nx/issues/16125

Also, I remember to see some tweaks somewhere to helps IDEs (such as VSCode and Volar Vue plugins) to correctly identify types and explicit auto-imports from external libraries.

I’ll try to create a fresh reproduction with that specific settings asap and link here.

Hmm 🤔

That clarifies a lot, one of my packages is a Nuxt@3 application, who basically works by doing some witchcraft on it’s own tsconfig in order to generate auto-imports, although the error itself is being thrown on a dependency library that have nothing special at all.

I don’t remember to deep import nothing from them, however, with that being said, maybe I can try to do some debugging to check if Nuxt isn’t trying to optimize something on build to save bundle size by deep importing files directly.