Error: Cannot find module '@angular/compiler-cli/src/perform_compile' when running ng-packagr

12,125

Hey recently solved a similar problem you can try following:

  1. Install latest node.js (Uninstall the previous version)

  2. Install the latest angular-cli globally

    npm uninstall -g angular-cli
    npm cache clean or npm cache verify (if npm > 5)
    npm install -g @angular/cli@latest
    
  3. Create a new project with angular-cli ng new projectname

  4. Copy your files (modules, component, directive etc.) into newly created project from your old one

  5. Install ng-packagr npm i ng-packagr

  6. Edit your package.json (root project)

    "scripts": {
        "packagr": "ng-packagr -p ng-package.json"
    }
    

    Remove dependencies or move all to peerDependencies since you are going to create a feature module

  7. ng-package.json (may be different in your case but you can try this)

    {
      "$schema": "./node_modules/ng-packagr/ng-package.schema.json",
      "lib": {
        "entryFile": "public_api.ts"
      }
    }
    
  8. public_api.ts (update relative path to your module)

    export * from './src/app/modules/yourmodulename/yourmodulename.module
    

And that's it now you can run npm run packagr it should work and generate dist folder.

You can publish on npmjs using npm publish dist

That's how I resolved it.

Share:
12,125
Scottie
Author by

Scottie

Updated on June 05, 2022

Comments

  • Scottie
    Scottie over 1 year

    I have a new install of windows and I'm trying to run ng-packagr for the first time.

    I'm getting the error:

    Error: Cannot find module '@angular/compiler-cli/src/perform_compile'
        at Function.Module._resolveFilename (module.js:547:15)
        at Function.Module._load (module.js:474:25)
        at Module.require (module.js:596:17)
        at require (internal/module.js:11:18)
        at Object.<anonymous> (C:\Users\MyUser\AppData\Roaming\npm\node_modules\ng-packagr\lib\ts\tsconfig.js:3:12)
        at Module._compile (module.js:652:30)
        at Object.Module._extensions..js (module.js:663:10)
        at Module.load (module.js:565:32)
        at tryModuleLoad (module.js:505:12)
        at Function.Module._load (module.js:497:3)
    

    I did install the angular CLI by running

    npm install -g @angular/cli@latest
    

    It's running v6.0.8

    My ng-package.json:

    {
        "$schema": "./node_modules/ng-packagr/ng-package.schema.json",
        "lib": {
            "entryFile": "index.ts",
            "externals": {
                "moment": "moment"
            }
        }
    }
    

    What am I missing here?

  • Scottie
    Scottie over 5 years
    Yeah,.I figured out you have to install ng-packagr v1.x or it won't work.