ng serve引发错误“ serve命令需要在Angular项目中运行,但找不到项目定义”

时间:2018-12-05 04:28:54

标签: angular

我需要运行该项目并在localhost上访问它。当我从gitlab下载项目并解压缩后,它发出了警告,指出本地和全局CLI软件包不匹配。然后,我将本地CLI版本升级到7.1.1。从那时起,它抛出此错误。全局和本地CLI软件包是否有问题?

我什至将全局和本地CLI版本降级到1.7.4。它抛出ng command not found错误。我的项目文件夹中有.angular-cli.json,而@angular/cli中的package.json是7.1.1。

从命令行运行ng serve时出现以下错误:

    C:\Users\mrunalini.prasad\Desktop\fsacademy\fsacademy-master>ng serve
    The serve command requires to be run in an Angular project, but a project definition could not be found.

我无法修复它。以下是我更新我的角度CLI全局和本地版本所遵循的步骤

    npm uninstall -g angular-cli
    npm uninstall --save-dev angular-cli

要将Angular CLI更新到新版本,必须同时更新全局软件包和项目的本地软件包。

    Global package:

    npm uninstall -g @angular/cli
    npm cache clean
    npm install -g @angular/cli@latest
    Local project package:

    rmdir node_modules on Windows
    npm install --save-dev @angular/cli@latest
    npm install

    ng --version is giving the below output:

    C:\Users\mrunalini.prasad\Desktop\fsacademy\fsacademy-master>ng --version

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


    Angular CLI: 7.1.1
    Node: 10.10.0
    OS: win32 x64
    Angular: 4.4.7
    ... animations, common, compiler, compiler-cli, core, forms
    ... http, platform-browser, platform-browser-dynamic, router
    ... tsc-wrapped

    Package                      Version
    ------------------------------------------------------
    @angular-devkit/architect    0.11.1
    @angular-devkit/core         7.1.1
    @angular-devkit/schematics   7.1.1
    @angular/cdk                 5.2.5
    @angular/cli                 7.1.1
    @angular/flex-layout         2.0.0-rc.1
    @angular/material            2.0.0-beta.5
    @schematics/angular          7.1.1
    @schematics/update           0.11.1
    rxjs                         5.5.12
    typescript                   2.3.4

能帮您解决此错误吗?

请在下面找到项目目录结构的屏幕快照:它具有.angular-cli.jsonpackage.json文件。

Directory of C:\Users\mrunalini.prasad\Desktop\fsacademy\fsacademy-master

12/05/2018  09:47 AM    <DIR>          .
12/05/2018  09:47 AM    <DIR>          ..
12/03/2018  03:24 PM             1,060 .angular-cli.json
12/03/2018  03:24 PM               245 .editorconfig
12/04/2018  03:12 PM    <DIR>          .vscode
12/03/2018  03:24 PM             2,272 app.component.ts
12/04/2018  03:12 PM    <DIR>          backup
12/05/2018  09:47 AM    <DIR>          node_modules
12/05/2018  09:47 AM           149,659 package-lock.json
12/04/2018  05:11 PM             1,299 package.json
12/03/2018  03:24 PM             1,397 package.json.2
12/03/2018  03:24 PM             3,427 README.md
12/04/2018  03:14 PM    <DIR>          src
12/04/2018  03:14 PM    <DIR>          steps
12/03/2018  03:24 PM               431 tsconfig.json
12/03/2018  03:24 PM             2,710 tslint.json
12/04/2018  03:14 PM    <DIR>          workshop-cli
12/03/2018  03:24 PM           174,967 yarn.lock
              10 File(s)        337,467 bytes
               8 Dir(s)  89,720,467,456 bytes free

我在运行npm install而未更新任何内容时收到以下错误:

gyp ERR! stack Error: Can't find Python executable "python", you can set the PYT
HON env variable.
gyp ERR! stack     at PythonFinder.failNoPython (C:\Users\mrunalini.prasad\Deskt
op\fsacademydemo\fsacademy-master\node_modules\node-gyp\lib\configure.js:484:19)

gyp ERR! stack     at PythonFinder.<anonymous> (C:\Users\mrunalini.prasad\Deskto
p\fsacademydemo\fsacademy-master\node_modules\node-gyp\lib\configure.js:509:16)
gyp ERR! stack     at C:\Users\mrunalini.prasad\Desktop\fsacademydemo\fsacademy-
master\node_modules\graceful-fs\polyfills.js:282:31
gyp ERR! stack     at FSReqWrap.oncomplete (fs.js:154:21)
gyp ERR! System Windows_NT 6.3.9600
gyp ERR! command "C:\\Program Files\\nodejs\\node.exe" "C:\\Users\\mrunalini.pra
sad\\Desktop\\fsacademydemo\\fsacademy-master\\node_modules\\node-gyp\\bin\\node
-gyp.js" "rebuild" "--verbose" "--libsass_ext=" "--libsass_cflags=" "--libsass_l
dflags=" "--libsass_library="
gyp ERR! cwd C:\Users\mrunalini.prasad\Desktop\fsacademydemo\fsacademy-master\no
de_modules\node-sass
gyp ERR! node -v v10.10.0
gyp ERR! node-gyp -v v3.8.0
gyp ERR! not ok

在npm安装后运行ng serve而不更新任何内容时,出现以下错误:

ERROR in ./node_modules/raw-loader!./node_modules/postcss-loader/lib??embedded!.
/node_modules/sass-loader/lib/loader.js??ref--8-3!./src/theme.scss
Module build failed: Error: Cannot find module 'node-sass'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:581:15)
    at Function.Module._load (internal/modules/cjs/loader.js:507:25)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at Object.<anonymous> (C:\Users\mrunalini.prasad\Desktop\fsacademydemo\fsaca
demy-master\node_modules\sass-loader\lib\loader.js:3:14)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at loadLoader (C:\Users\mrunalini.prasad\Desktop\fsacademydemo\fsacademy-mas
ter\node_modules\loader-runner\lib\loadLoader.js:13:17)
    at iteratePitchingLoaders (C:\Users\mrunalini.prasad\Desktop\fsacademydemo\f
sacademy-master\node_modules\loader-runner\lib\LoaderRunner.js:169:2)
    at iteratePitchingLoaders (C:\Users\mrunalini.prasad\Desktop\fsacademydemo\f
sacademy-master\node_modules\loader-runner\lib\LoaderRunner.js:165:10)
    at C:\Users\mrunalini.prasad\Desktop\fsacademydemo\fsacademy-master\node_mod
ules\loader-runner\lib\LoaderRunner.js:173:18

2 个答案:

答案 0 :(得分:2)

似乎您尚未将项目迁移到版本7。新的角度cli使用angular.json而非.angular-cli.json进行配置。

遵循migration guide

答案 1 :(得分:0)

如果您不想更新任何东西而只想运行,请按照以下步骤操作,

  • 更新角度npm install @angular/cli -g的全局版本
  • 将新克隆文件放入另一个文件夹中(因为使用--save后,您的package.json已更改)
  • 使用npm install,现在会出现不匹配错误
  • 只需使用此ng config -g cli.warnings.versionMismatch false

我希望这会有所帮助。