【问题标题】:Ubuntu how to run ng commndUbuntu如何运行ng命令
【发布时间】:2018-09-21 11:12:53
【问题描述】:

在 Ubuntu 16.04 中,我使用以下命令安装了 nodejs:

sudo apt-get update
sudo apt-get install nodejs
sudo apt-get install npm

为了确认,我使用了命令which nodejs,它返回了

/usr/bin/nodejs

我想运行一个 Angular 项目。我还创建了“node_modules”文件夹并更新了“Angular CLI”。但是,ng 命令在终端中不起作用。

已编辑

下面是我的 package.json 内容:

{
  "name": "angular",
  "version": "0.0.0",
  "license": "MIT",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular-devkit/core": "^0.2.0",
    "@angular/animations": "^5.0.0",
    "@angular/common": "^5.0.0",
    "@angular/compiler": "^5.0.0",
    "@angular/core": "^5.0.0",
    "@angular/forms": "^5.0.0",
    "@angular/http": "^5.0.0",
    "@angular/platform-browser": "^5.0.0",
    "@angular/platform-browser-dynamic": "^5.0.0",
    "@angular/router": "^5.0.0",
    "bootstrap": "^3.3.7",
    "core-js": "^2.4.1",
    "rxjs": "^5.5.2",
    "zone.js": "^0.8.14"
  },
  "devDependencies": {
    "@angular/cli": "^1.6.7",
    "@angular/compiler-cli": "^5.0.0",
    "@angular/language-service": "^5.0.0",
    "@types/jasmine": "~2.5.53",
    "@types/jasminewd2": "~2.0.2",
    "@types/node": "~6.0.60",
    "codelyzer": "^4.0.1",
    "jasmine-core": "~2.6.2",
    "jasmine-spec-reporter": "~4.1.0",
    "karma": "~1.7.0",
    "karma-chrome-launcher": "~2.1.1",
    "karma-cli": "~1.0.1",
    "karma-coverage-istanbul-reporter": "^1.2.1",
    "karma-jasmine": "~1.1.0",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.1.2",
    "ts-node": "~3.2.0",
    "tslint": "~5.7.0",
    "typescript": "~2.4.2"
  }
}

空白终端在下面的屏幕中给出:

我看到运行命令npm install -g @angular/cli后出现以下错误:

npm 错误!错误:EACCES:权限被拒绝,访问 '/usr/local/lib' npm 呃!在错误(本机)npm ERR! { [错误:EACCES:权限 拒绝,访问 '/usr/local/lib'] npm ERR!错误号:-13,npm 错误!
代码:'EACCES',npm 错误!系统调用:'访问',npm ERR!小路: '/usr/local/lib' } npm 错误! npm 错误!请尝试运行此命令 再次以 root/管理员身份。

已编辑 2


现在我在放置 ng serve 命令后看到:

niladri@niladrib:/var/www/html/ngAngular$ ng 服务 /usr/local/lib/node_modules/@angular/cli/models/config/config.js:17 构造函数(_configPath,架构,configJson,后备= []){ ^

SyntaxError: Unexpected token = 在exports.runInThisContext (vm.js:53:16) 在 Module._compile (module.js:374:25) 在 Object.Module._extensions..js (module.js:417:10) 在 Module.load (module.js:344:32) 在 Function.Module._load (module.js:301:12) 在 Module.require (module.js:354:17) 在要求(内部/module.js:12:17) 在对象。 (/usr/local/lib/node_modules/@angular/cli/models/config.js:3:18) 在 Module._compile (module.js:410:26) 在 Object.Module._extensions..js (module.js:417:10)

【问题讨论】:

标签: angularjs ubuntu ubuntu-16.04


【解决方案1】:

Angular-cli 有一定的先决条件,其中包括节点版本 > 6.9,如下所示:

https://github.com/angular/angular-cli#prerequisites

要将节点更新到最新版本,我们可以关注:

https://askubuntu.com/questions/426750/how-can-i-update-my-nodejs-to-the-latest-version

【讨论】:

    【解决方案2】:

    嘿@Shivi 首先安装 npm install -g sass node-sass。这在全球范围内。然后使用“npm install”进入ngAngular文件夹安装依赖项,这在ng serve运行项目后就足够了。如果它运行的图像 enter image description here

    【讨论】:

      【解决方案3】:

      您需要全局安装 AngularCLI 才能使用 ng serve 提供应用程序。

      为此,请在您的终端中运行以下命令:
      npm install -g @angular/cli(如果您需要 Ubuntu 的管理员权限,请使用 sudo。)

      这将全局安装 AngularCLI。然后,您可以通过导航到文件夹并输入 ng s 来简单地使用它来为应用程序提供服务。

      【讨论】:

      • 实际上我看到我的 nodejs 版本是 4。如何按照 Shivi 上面的建议将它升级到至少 6.9?
      • @NiladriBanerjee-Uttarpara 这应该会为您更新节点和 npm。 askubuntu.com/questions/426750/…
      • 我已经更新了我的问题。能否请您进一步提出建议。
      • 您需要以超级用户身份运行。 sudo npm install -g @angular/cli我会更新我的答案。
      • 你是否安装了最新版本的node?如果是这样,请尝试再次运行 npm install。
      【解决方案4】:

      好的,首先写到你的终端 ng --version,这样你会得到你在桌面上安装的 angular cli 的版本,并将它与这个 "@angular / cli": "^ 1.6.7" 进行比较如果你的angular cli的版本在底部,更新它,如何做在文档中,然后在终端中写入sudo npm install,以便在运行ng serve后安装Json包文件的所有依赖项

      【讨论】:

      • 我转到我的项目所在的文件夹,例如 /var/www/html/ngAngular/ 并编写此命令 ng --version,它显示一个空白终端
      • 我已经编辑了我的问题并添加了一个屏幕截图以供您帮助。
      • 下面是我给你的解决方案
      【解决方案5】:

      检查 json 包文件,查看脚本部分,在终端中写入启动后的文本。在同一个文件中的示例,您可以在文件夹 node_modules 中看到已安装的依赖项

      【讨论】:

      • 我已经编辑了我的问题。您能否检查并解释我需要在终端中编写什么命令?请通过编辑您的答案来解释它。
      猜你喜欢
      • 2021-02-27
      • 2014-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-03
      • 1970-01-01
      相关资源
      最近更新 更多