无法在 Angular 2 中通过 npm 安装依赖项

Posted

技术标签:

【中文标题】无法在 Angular 2 中通过 npm 安装依赖项【英文标题】:Can't install dependencies via npm in Angular 2 【发布时间】:2017-06-03 07:39:36 【问题描述】:

我已经从主站点 (quickproject) 下载了 Angular 2,但我在安装其他库时遇到了很多问题。例如,当我尝试安装 angular2-modal 模块时,我得到:

 npm install angular2-modal --save
angular-quickstart@1.0.0 C:\WebServ\httpd\quickstart
`-- angular2-modal@2.0.2

npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@^1.0.0 (node_modules\chokidar\node_modules\fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.0.17: wanted "os":"darwin","arch":"any" (current: "os":"win32","arch":"x64")
npm WARN @angular/upgrade@2.2.4 requires a peer of @angular/core@2.2.4 but none was installed.
npm WARN @angular/upgrade@2.2.4 requires a peer of @angular/compiler@2.2.4 but none was installed.
npm WARN @angular/upgrade@2.2.4 requires a peer of @angular/platform-browser@2.2.4 but none was installed.
npm WARN @angular/upgrade@2.2.4 requires a peer of @angular/platform-browser-dynamic@2.2.4 but none was installed.

由于我认为这个 fsevents,我无法安装任何东西。当我尝试这样做时

npm update 

我收到了

npm update --save
npm ERR! Windows_NT 6.1.7601
npm ERR! argv "D:\\Program Files\\nodejs\\node.exe" "D:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "update" "--save"
npm ERR! node v6.9.2
npm ERR! npm  v3.10.9
npm ERR! code EBADPLATFORM

npm ERR! notsup Unsupported platform for fsevents@1.0.17: wanted "os":"darwin","arch":"any" (current: "os":"win32","arch":"x64")
npm ERR! notsup Valid OS:    darwin
npm ERR! notsup Valid Arch:  any
npm ERR! notsup Actual OS:   win32
npm ERR! notsup Actual Arch: x64

npm ERR! Please include the following file with any support request:
npm ERR!     C:\WebServ\httpd\quickstart\npm-debug.log

如何修复?




"name": "angular-quickstart",
  "version": "1.0.0",
  "description": "QuickStart package.json from the documentation, supplemented with testing support",
  "scripts": 
    "start": "tsc && concurrently \"tsc -w\" \"lite-server\" ",
    "e2e": "tsc && concurrently \"http-server -s\" \"protractor protractor.config.js\" --kill-others --success first",
    "lint": "tslint ./app/**/*.ts -t verbose",
    "lite": "lite-server",
    "pree2e": "webdriver-manager update",
    "test": "tsc && concurrently \"tsc -w\" \"karma start karma.conf.js\"",
    "test-once": "tsc && karma start karma.conf.js --single-run",
    "tsc": "tsc",
    "tsc:w": "tsc -w"
  ,
  "keywords": [],
  "author": "",
  "license": "MIT",
  "dependencies": 
    "@angular/common": "^2.4.3",
    "@angular/compiler": "^2.4.3",
    "@angular/core": "^2.4.3",
    "@angular/forms": "^2.4.3",
    "@angular/http": "^2.4.3",
    "@angular/platform-browser": "^2.4.3",
    "@angular/platform-browser-dynamic": "^2.4.3",
    "@angular/router": "^3.4.3",
    "@angular/upgrade": "^2.2.4",
    "@swimlane/ngx-datatable": "^5.0.0",
    "@types/jasmine": "^2.5.41",
    "@types/node": "^6.0.60",
    "angular-datatables": "^2.0.1",
    "angular-in-memory-web-api": "~0.2.4",
    "angular2-datatable": "^0.5.2",
    "fsevents": "^1.0.0",
    "angular2-modal": "^2.0.2",
    "core-js": "^2.4.1",
    "datatables.net": "^2.1.1",
    "datatables.net-dt": "^2.1.1",
    "jquery": "^3.1.1",
    "karma": "^1.4.0",
    "rxjs": "^5.0.3",
    "systemjs": "0.19.40",
    "zone.js": "^0.7.6"
  ,
  "devDependencies": 
    "concurrently": "^3.1.0",
    "lite-server": "^2.2.2",
    "typescript": "~2.0.10",
    "canonical-path": "0.0.2",
    "http-server": "^0.9.0",
    "tslint": "^3.15.1",
    "lodash": "^4.16.4",
    "jasmine-core": "~2.4.1",
    "karma": "^1.3.0",
    "karma-chrome-launcher": "^2.0.0",
    "karma-cli": "^1.0.1",
    "karma-jasmine": "^1.0.2",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~4.0.14",
    "rimraf": "^2.5.4",
    "@types/node": "^6.0.46",
    "@types/jasmine": "^2.5.36"
  ,
  "repository": 

【问题讨论】:

【参考方案1】:

通过 NPM 下载工作 Angular2 的简单步骤

    在系统中安装 NPM 将 node.js 安装到您的系统中 现在按照以下步骤操作:-

注意:- 全局安装 Angular2 CLI 命令:npm install -g @angular/cli

Step.1 npm install --save-dev @angular/cli@latest(安装角度依赖)

Step.2 ng new my-app(创建新的 Angular2 项目)

Step.3 cd my-app(来到你的项目路径)

Step.4 ng serve --open(该命令用于启动服务器)

打开你的 Angular2 项目:- http://localhost:4200/

【讨论】:

【参考方案2】:

从 node_modules 中删除所有内容。从终端运行“npm install”。然后尝试运行“npm start”。确保你已经安装了最新版本的 node & npm

【讨论】:

【参考方案3】:

fsevents 是一个特定于 OS X 的 API,可以在该操作系统上更好地处理文件通知事件。由于您在 Windows 上,因此无法运行 fsevents。它仅适用于 OS X。

您收到的npm WARN 消息只是警告。它跳过了您无法在操作系统上安装的可选 fsevents 依赖项。您收到的其他一些警告是您要安装的 Angular2 (2.4.3) 和 angular2-modal 想要作为对等设备 (2.2.4) 的版本不匹配,但这应该可能 工作正常。

package.json 中删除fsevents。其他一些依赖项可能会尝试在 OS X 上安装它,但您不需要将其作为项目的核心依赖项包含在内。

【讨论】:

以上是关于无法在 Angular 2 中通过 npm 安装依赖项的主要内容,如果未能解决你的问题,请参考以下文章

尝试从 npm 安装 Angular 2 Material - 源文本中无法识别的标记

如何使用 NPM 安装 Angular 2?

微信小程序中通过npm引用第三方插件

无法使用 npm 安装 @angular/cli

我无法使用 npm install 安装我的特定 Angular 项目之一

无法使用 Windows 10 安装 Firebase 工具 cli