Search code examples
angulartypescriptwebpackng-packagr

TypeError [ERR_INVALID_ARG_VALUE] while trying to import module from my library in Angular 6


I'm trying to use my created Angular 6 library in an application. I was able to generate, configure and compile the library following this guide, but I can't seem to use it in another Angular project. I created a local package with npm pack and installed it in the Angular app with npm install using the .tgz file path. Here's some info:

Library's tsconfig.json

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "module": "es2015",
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "typeRoots": ["node_modules/@types"],
    "lib": ["es2017", "dom"],
    "paths": {
      "lib-components": ["dist/lib-components"],
      "lib-components/*": ["dist/lib-components/*"]
    }
  }
}

public_api.ts

/*
 * Public API Surface of lib-components
 */

export * from './lib/components/spinner/spinner.module';

Import statement in the consumer app:

import { BootstrapSpinnerModule } from 'lib-components';

The error:

TypeError [ERR_INVALID_ARG_VALUE]: The argument 'path' must be a string or
Uint8Array without null bytes. 
Received 'path_to_app_root/src/\u0000commonjsHelpers.js/package.json'
    at readFile (fs.js:296:3)
...

Versions:

"@angular/cli": "~6.2.9" in both consumer and library

"@angular/*": "6.1.0" in both consumer and library

I've tried every suggestion that I found in Github issues, with no luck.


Solution

  • Creating a new library from scratch seems to have fixed it. Can't pinpoint the exact reason it wasn't working in the first place, but here are some steps I took in case you stumble in the same problem in the future:

    • Make sure your angular-cli version is the latest stable one for your major version (had to update from 6.1.x to 6.2.9), and remember that your local cli version overrides the global one, so you'll probably have to update both (in the question I say that I'm using the latest cli version, but I generated the library with version 6.1.x and after everything broke I tried to update the cli. Don't do that. If you update your cli, generate a new library.
    • Don't know if this is a thing, but I renamed the library in case I was accidentally using some kind of reserved word (from lib-components to my-company-components)