【问题标题】:Error: Cannot find module '@angular/compiler-cli/src/perform_compile' when running ng-packagr错误:运行 ng-packagr 时找不到模块“@angular/compiler-cli/src/perform_compile”
【发布时间】:2018-12-11 00:47:20
【问题描述】:

我有一个新安装的 Windows,我正在尝试第一次运行 ng-packagr。

我收到了错误:

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)

我确实通过运行安装了 angular CLI

npm install -g @angular/cli@latest

它正在运行 v6.0.8

我的 ng-package.json:

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

我在这里错过了什么?

【问题讨论】:

    标签: npm angular-cli ng-packagr


    【解决方案1】:

    嘿最近解决了一个类似的问题,您可以尝试以下操作:

    1. 安装最新的node.js(卸载之前的版本)

    2. 全局安装最新的 angular-cli

      npm uninstall -g angular-cli
      npm cache clean or npm cache verify (if npm > 5)
      npm install -g @angular/cli@latest
      
    3. 使用 angular-cli 创建一个新项目 ng new projectname

    4. 将您的文件(模块、组件、指令等)从旧文件复制到新创建的项目中

    5. 安装 ng-packagr npm i ng-packagr

    6. 编辑您的package.json(根项目)

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

      删除依赖项或将所有依赖项移至 peerDependencies,因为您要创建一个功能模块

    7. ng-package.json(在你的情况下可能会有所不同,但你可以试试这个)

      {
        "$schema": "./node_modules/ng-packagr/ng-package.schema.json",
        "lib": {
          "entryFile": "public_api.ts"
        }
      }
      
    8. public_api.ts(更新模块的相对路径)

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

    就是这样,现在您可以运行 npm run packagr 它应该可以工作并生成 dist 文件夹。

    您可以使用 npm publish dist 在 npmjs 上发布

    我就是这样解决的。

    【讨论】:

    • 是的,我发现你必须安装 ng-packagr v1.x 否则它将无法工作。
    猜你喜欢
    • 2020-05-22
    • 2020-11-21
    • 2021-08-10
    • 1970-01-01
    • 2020-05-03
    • 2018-02-04
    • 2020-05-25
    • 1970-01-01
    • 2019-07-03
    相关资源
    最近更新 更多