angular: Cannot read property 'bindingStartIndex' of null when using Library

🐞 bug report

Affected Package

Not sure. Maybe this is somewhere in @angular/cli or somewhere related to Webpack.

Is this a regression?

Yes, the previous version in which this bug was not present was Angular 8

Description

When creating a library as described in the documentation and using it in an empty app, I get the following error at runtime:

ERROR TypeError: Cannot read property 'bindingStartIndex' of null
at Module.ɔɔelementStart (core.js:20997)
at MyLibComponent_Template (my-lib.js:19)
at executeTemplate (core.js:11930)
at renderView (core.js:11716)
at renderComponent (core.js:13232)
at renderChildComponents (core.js:11519)
at renderView (core.js:11742)
at renderComponent (core.js:13232)
at renderChildComponents (core.js:11519)
at renderView (core.js:11742)

I do not see any errors while compiling.

🔬 Minimal Reproduction

I did the following:

  • Create a my-libs project: ng new my-libs --create-application=false
  • Create a library: cd my-libs and ng generate library my-lib
  • Create an app next to this: cd .. and ng new my-app
  • Add the library as a dependency of the app: Add "my-lib": "file:../my-libs/dist/my-lib" to the dependencies in the app’s package.json and run npm install
  • Import the MyLibModule in the AppModule (imported via import { MyLibModule } from 'my-lib';)
  • Use the component in the AppComponent: <lib-my-lib></lib-my-lib>
  • Build the library: cd my-libs and ng build --prod
  • Run the app: cd .. and ng serve
  • Point browser at http://localhost:4200/
  • Open Developer Tools to look at the console

The resulting project is contained in this zip-file repro.zip. I removed the node_modules, so please do

$ cd my-libs
$ npm install
$ ng build --prod
$ cd ..
$ cd my-app
$ npm install
$ ng serve

đŸ”„ Exception or Error


    ERROR TypeError: Cannot read property 'bindingStartIndex' of null
    at Module.ɔɔelementStart (core.js:20997)
    at MyLibComponent_Template (my-lib.js:19)
    at executeTemplate (core.js:11930)
    at renderView (core.js:11716)
    at renderComponent (core.js:13232)
    at renderChildComponents (core.js:11519)
    at renderView (core.js:11742)
    at renderComponent (core.js:13232)
    at renderChildComponents (core.js:11519)
    at renderView (core.js:11742)

🌍 Your Environment

Angular Version:


     _                      _                 ____ _     ___
    / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
   / △ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
  / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
 /_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
                |___/
    

Angular CLI: 9.0.2
Node: 12.13.0
OS: darwin x64

Angular: 
... 
Ivy Workspace: 

Package                      Version
------------------------------------------------------
@angular-devkit/architect    0.900.2
@angular-devkit/core         9.0.2
@angular-devkit/schematics   9.0.2
@schematics/angular          9.0.2
@schematics/update           0.900.2
rxjs                         6.5.3

About this issue

  • Original URL
  • State: open
  • Created 7 years ago
  • Reactions: 33
  • Comments: 32 (5 by maintainers)

Commits related to this issue

Most upvoted comments

in your angular app that depends on your local library, set projects.projectName.architect.build.options.preserveSymlinks with true in angular.json will prevent the angular cli from updating linked local library

This happens when @NgModule() is resolved from a different node_modules location.

Given this configuration, we can use my-lib from my-app directly without building my-lib:

# project directory structure
/
  /my-app
    /node_modules
    /projects
  /my-lib
    /node_modules
    /projects

# my-app's tsconfig.json:
    "paths": {
      "my-lib": [
        "../my-lib/projects/my-lib/src/public-api"
      ]
    }

As @mhevery highlighted, the error indicates that somehow there are two copies of Ivy in there. This was due to TypeScript’s module resolution approach - it looks for node_modules by traversing up the directory of file being compiled.

To fix this, I tell Typescript where to find Angular packages by using:

    "paths": {
      "@angular/*": [
        "./node_modules/@angular/*"
      ],
      "my-lib": [
        "../my-lib/projects/my-lib/src/public-api"
      ]
    }

Initial test seems to be working. Hope this helps.

Also, I think this approach does not depend on preserveSymlinks or "enableIvy": false.

Specifying path to angular packages as @kctang mentioned was enough for me to fix the error, thank you for that. I just wanna point out that the path belongs in the compilerOptions section of app’s tsconfig.json file.

// app's tsconfig.json
{
  ...
  "compilerOptions": {
    ...
    "paths": {
      "@angular/*": [
        "./node_modules/@angular/*"
      ]
    }
  }
}

For now I guess I’ll have to create a little schematic for my co-workers to spare them the trouble, but I really hope this gets fixed soon tho.

Edit: I should probably read documentation twice next time.

Edit2: Oh and specifying path to lib’s source code to skip building is actually really useful. Big ups to @kctang for that as well!

Ok, this issue only happens when you link the library (or use file:... in your package.json).

When you build the library with ng build my-lib --prod and manually copy the dist/my-lib into node_modules/my-lib (thus not creating a symlink) the ng serve is not throwing an error and everything works.

If you use npm link, you can see that the original library folder (dist/my-lib) gets changed on ng serve, also the package.json will be modified, for example this is added:

  "scripts": {
    "prepublishOnly": "node --eval \"console.error('ERROR: Trying to publish a package that has been compiled by NGCC. This is not allowed.\\nPlease delete and rebuild the package, without compiling with NGCC, before attempting to publish.\\nNote that NGCC may have been run by importing this package into another project that is being built with Ivy enabled.\\n')\" && exit 1"
  }

The problem lies here somewhere. If you publish the package to npm (or github), this won’t be an issue. I think if you create a read-only symlink to my-lib, this will work as it will not modify the original package?

In summary: npm link with custom libraries is currently broken (I think only with 9.x.x?). When ng serve is being run, the linked package gets modified and thus broken.

I have the same issue. I have different workspaces for library and app.

I’ve been facing the same issue in a recent project
 In this case though

"preserveSymlinks": true,
"paths": {
  "@angular/*": ["./node_modules/@angular/*"]
}

Doesn’t help!

The difference with the other projects, where it worked is that in this case we are working with angular 17 with the new build system and vite

Does this now need a different workaround?

@00guille I got it working by adding this option to tsconfig.json of both the library repository and the main app repository:

  "angularCompilerOptions": {
    "enableIvy": false
  }

I haven’t tested all the possible configurations (like using Ivy in the main App and not using it in the library or vice versa).

I can now link the dependency locally and it works. Hope this helps for now.

For anyone struggling with Angular17 + esbuild.

  1. Follow the steps TO THE POINT to introduce esbuild to your app https://v17.angular.io/guide/esbuild#for-existing-applications
  2. Remove any node module references in your paths in the application tsconfig.json. I Had "@angular/*": ["node_modules/@angular/*"],, which I had to remove. Why? Typescript -> https://www.typescriptlang.org/docs/handbook/modules/reference.html#paths-should-not-point-to-monorepo-packages-or-node_modules-packages

I am tired so this might not be all


My actions that helped me solve the issue, if the above didn’t help.

  1. Delete all my code, until I have only a single <div> in my app.component (this helped me understand that’s it’s a config issue)
  2. Create a new angular17 app and check if it’s running successfully (it is, truly a config issue)
  3. Combine my configs with the new app configs. The new app configs take precedence as it’s working
  4. See that my app now works with the config from the fresh angular17 barebones app
  5. Step by step revert changes until I break it to understand which specific config creates the issue

Overall, I’d say I am not happy with this bruteforce method, and I could mainly do it because my app is simple. I could and did get away without understanding how things work. A bad long-term lesson.

Conclusion: Learn.

it worked! but the idea is to be able to use ivy with the benefits that it carries

We experienced this issue when updating angular to v18, but only got the ‘bindingStartIndex’ issue when running ng serve. If the dist folder is copied and served from our own http server, this issue does not occur and the page loads correctly. Another workaround we found that does seem to work, is running it as ng serve --prebundle=false

I hit the same issue trying to have a multi project setup with an ionic project and an angular project trying to share a common library.

this answer solved the problem for me but I was wondering if it has any downsides to directly referencing the library like " “
/my-lib/projects/my-lib/src/public-api”" rather than looking for the version in the dist folder.

Because this solves a couple of issue for me which I find very useful,

  1. not able to find all references from the library
  2. “go to definition” you will end up in the dist folder than the actual code.

This happens when @NgModule() is resolved from a different node_modules location.

Given this configuration, we can use my-lib from my-app directly without building my-lib:

# project directory structure
/
  /my-app
    /node_modules
    /projects
  /my-lib
    /node_modules
    /projects

# my-app's tsconfig.json:
    "paths": {
      "my-lib": [
        "../my-lib/projects/my-lib/src/public-api"
      ]
    }

As @mhevery highlighted, the error indicates that somehow there are two copies of Ivy in there. This was due to TypeScript’s module resolution approach - it looks for node_modules by traversing up the directory of file being compiled.

To fix this, I tell Typescript where to find Angular packages by using:

    "paths": {
      "@angular/*": [
        "./node_modules/@angular/*"
      ],
      "my-lib": [
        "../my-lib/projects/my-lib/src/public-api"
      ]
    }

Initial test seems to be working. Hope this helps.

Also, I think this approach does not depend on preserveSymlinks or "enableIvy": false.

Specifying path to angular packages as @kctang mentioned was enough for me to fix the error, thank you for that. I just wanna point out that the path belongs in the compilerOptions section of app’s tsconfig.json file.

// app's tsconfig.json
{
  ...
  "compilerOptions": {
    ...
    "paths": {
      "@angular/*": [
        "./node_modules/@angular/*"
      ]
    }
  }
}

For now I guess I’ll have to create a little schematic for my co-workers to spare them the trouble, but I really hope this gets fixed soon tho.

Edit: I should probably read documentation twice next time.

Edit2: Oh and specifying path to lib’s source code to skip building is actually really useful. Big ups to @kctang for that as well!

this actually fixed my same issue

The two most common tips for this problem are:

  1. Changing the paths array inside tsconfig.json and
  2. Setting “preserveSymlinks”: true inside angular.json.

For me in Angular 17, I had to delete "@angular/*": ["./node_modules/@angular/*"] from tsconfig.json but leave “preserveSymlinks”: true

@Yabonev comment led me to the solution.

in your angular app that depends on your local library, set projects.projectName.architect.build.options.preserveSymlinks with true in angular.json will prevent the angular cli from updating linked local library

“projects”: { “your-project-name”: { “architect”: { “build”: { “options”: { “preserveSymlinks”: true, // other existing options } } } } }

when you are building an angular library and importing it outside your angular workspace, you have to use this “preserveSymlinks”: true , and it will solve the error

ERROR Error: Uncaught (in promise): TypeError: Cannot read property ‘bindingStartIndex’ of null TypeError: Cannot read property ‘bindingStartIndex’ of null Screenshot (115)

I found out that the problem was that I’m using the --prod option in my library build command the solution was to

  • remove the --prod option
  • set "preserveSymlinks": true and suggested here
  • and enable Ivy "enableIvy": true

I hit the same issue trying to have a multi project setup with an ionic project and an angular project trying to share a common library.

this answer solved the problem for me but I was wondering if it has any downsides to directly referencing the library like " “
/my-lib/projects/my-lib/src/public-api”" rather than looking for the version in the dist folder.

Because this solves a couple of issue for me which I find very useful,

  1. not able to find all references from the library
  2. “go to definition” you will end up in the dist folder than the actual code.

This happens when @NgModule() is resolved from a different node_modules location. Given this configuration, we can use my-lib from my-app directly without building my-lib:

# project directory structure
/
  /my-app
    /node_modules
    /projects
  /my-lib
    /node_modules
    /projects

# my-app's tsconfig.json:
    "paths": {
      "my-lib": [
        "../my-lib/projects/my-lib/src/public-api"
      ]
    }

As @mhevery highlighted, the error indicates that somehow there are two copies of Ivy in there. This was due to TypeScript’s module resolution approach - it looks for node_modules by traversing up the directory of file being compiled. To fix this, I tell Typescript where to find Angular packages by using:

    "paths": {
      "@angular/*": [
        "./node_modules/@angular/*"
      ],
      "my-lib": [
        "../my-lib/projects/my-lib/src/public-api"
      ]
    }

Initial test seems to be working. Hope this helps. Also, I think this approach does not depend on preserveSymlinks or "enableIvy": false.

I also have different workspaces for library and app. I mean the fix with TypeScript-paths is kinda straightfoward and I did of course. But somehow I still have the same error message while rendering my component template and importing Modules (Services etc) works fine.

So I just imported in app.module.ts my created component from library. like this: import { LibComponent } from '../../../../myLib/projects/myLib/src/lib/components/libComponent/lib.component'; And I still get the same error message. I mean how is this possible right here?

I just run into the same issue. Our setup is a little bit more complex, since we are loading the library via fetch from an external source, then require it (electron require in our angular application) and load the library with the following snippet.

const result: any = window.require(pathToFetchedAngularUMDBundle);

this._compiler.compileModuleAsync(result.BusinessLogicModule).then((moduleFactory) => {
  try {
    const moduleRef: any = moduleFactory.create(this._injector);
    const component = moduleRef.instance.getComponent();
    const componentFactory = moduleRef.componentFactoryResolver.resolveComponentFactory(result.BusinessLogicComponent);
    const { instance } = this.container.createComponent(componentFactory, null, moduleRef.injector);
  } catch (err) {
    console.error('error', err);
  }
});

If I disable Ivy in both, the library and the application, everything works as expected (took me quite a while to get there to be honest). As soon as Ivy is enabled, I get the error mentioned in this issue. I tried using preserveSymlinks which does not change anything.

Has anyone an idea what might help in my setup? Unfortunately it is quite difficult to provide an example, but if it helps I can of course try to create something.