ng new my-app unexpected token =

2019-02-16 04:51发布

问题:

I have installed the angular2 cli via npm, but when I try to create a new typescript angular app with the command 'ng new my-app' I keep getting this error:

C:\Users\nicholas\AppData\Roaming\npm\node_modules\@angular\cli\models\config\config.js:15
    constructor(_configPath, schema, configJson, fallbacks = []) {
                                                           ^

SyntaxError: Unexpected token =
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:373:25)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Module.require (module.js:353:17)
    at require (internal/module.js:12:17)
    at Object.<anonymous> (C:\Users\nicholas\AppData\Roaming\npm\node_modules\@angular\cli\models\config.js:2:18)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)

My NPM version is 4.1.2 My node version is 4.4.5 which from my understanding are acceptable versions for installing and using the angular-cli.

Please let me know if there is a fix to this, I have also tried uninstalling, cleaning npm cache and reinstalling but I keep getting the same issue.

Any and all help is very appreciated, thanks in advance!

回答1:

If you have installed @angular/cli, you need NodeJS higher 6.9.7, together with NPM 3 or higher.

If you have installed angular-cli, you need NodeJS higher than 4.4.x, together with NPM 3 or higher.

For more see the links above and read the prerequisites.



回答2:

As of 2017-06-03, using Angular-CLI 1.1.0, NodeJS 7.10.0, npm 5.0.2, Angular 4.1.3, TypeScript 2.3.4, I experienced the error of the Original Poster (OP) in Visual Studio 2017 during Task Runner Explorer, running the build task (ng build as defined in package.json).

I launched a PowerShell prompt, navigated to the directory where the package.json lives, and invoked ng build from the command line, and it experienced zero errors.

That immediately raised my suspicions that it was a PATH (within VS 2017) issue that was causing the error to appear in VS 2017's Task Runner output window.

Sure enough, under Tools / Options / Projects and Solutions / Web Package Management / External Tools, I had to move the entry for $(PATH) up above $(VSINSTALLDIR)\Web\External and (in my case at least) below .\node_modules.bin.

Once I did all of the above, I was able to right-click on build (under the Custom tab) of Task Runner Explorer and this time it was successful.

Before anybody replies, keep in mind the following:

  1. This is VERY SPECIFIC to Visual Studio 2017 in which an Angular 4.1.3 application is being developed to (hopefully) peacefully co-exist with ASP.Net Core 1.1.
  2. I have TypeScript 2.3 globally installed and locally installed.
  3. I have the very latest NodeJS and NPM editions (as of the time of writing this post at least).
  4. The techniques I used that resolved this issue for me may not work for you.

Bottom line: it might be the order of where things are in your PATH that might be causing the error.



回答3:

solved as followes:

do

ashish@ashish-Inspiron-3521:~$ sudo su [sudo] password for ashish: root@ashish-Inspiron-3521:/home/ashish# sudo npm cache clean -f npm WARN using --force I sure hope you know what you are doing. root@ashish-Inspiron-3521:/home/ashish# sudo npm install -g n /usr/local/bin/n -> /usr/local/lib/node_modules/n/bin/n /usr/local/lib └── n@2.1.8

root@ashish-Inspiron-3521:/home/ashish# sudo n stable

 install : node-v9.8.0
   mkdir : /usr/local/n/versions/node/9.8.0
   fetch : https://nodejs.org/dist/v9.8.0/node-v9.8.0-linux-x64.tar.gz
################################################################## 100.0% ################################################################## 100.0%

installed : v9.8.0

root@ashish-Inspiron-3521:/home/ashish# root@ashish-Inspiron-3521:/home/ashish# sudo ln -sf /usr/local/n/versions/node/9.8.0/bin/node /usr/bin/nodejs root@ashish-Inspiron-3521:/home/ashish# sudo n latest

 install : node-v9.9.0
   mkdir : /usr/local/n/versions/node/9.9.0
   fetch : https://nodejs.org/dist/v9.9.0/node-v9.9.0-linux-x64.tar.gz
################################################################## 100.0%

installed : v9.9.0

root@ashish-Inspiron-3521:/home/ashish# root@ashish-Inspiron-3521:/home/ashish# ng -v

_                      _                 ____ _     ___

/ \ _ __ __ _ _ _| | __ _ _ __ / | | | | / △ \ | ' \ / | | | | |/ _ | '| | | | | | | / \| | | | (| | || | | (| | | | || |_ | | // __| ||__, |__,||__,|| __|_____|| |/

Angular CLI: 1.7.3 Node: 9.9.0 OS: linux x64 Angular: ... root@ashish-Inspiron-3521:/home/ashish# node -v v9.9.0 root@ashish-Inspiron-3521:/home/ashish#