Freshly installed Angular Cli gives error rather t

2019-07-11 06:26发布

问题:

I have been trying to install the latest angular-cli in order to start a new project, but I've been encountering a very strange issue. When I run

npm install -g angular-cli@latest

angular-cli will seem to install successfully. However, when I actually try to use it or check the version with

ng --version

I get this error:

fs.js:640
  return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode);
             ^

TypeError: path must be a string or Buffer
    at TypeError (native)
    at Object.fs.openSync (fs.js:640:18)
    at Object.fs.readFileSync (fs.js:508:33)
    at Function.Version.fromProject (/Users/ryan/.nvm/versions/node/v6.7.0/lib/node_modules/angular-cli/upgrade/version.js:87:31)
    at Function.Version.isPreWebpack (/Users/ryan/.nvm/versions/node/v6.7.0/lib/node_modules/angular-cli/upgrade/version.js:111:31)
    at Function.Version.assertPostWebpackVersion (/Users/ryan/.nvm/versions/node/v6.7.0/lib/node_modules/angular-cli/upgrade/version.js:97:18)
    at /Users/ryan/.nvm/versions/node/v6.7.0/lib/node_modules/angular-cli/bin/ng:25:15
    at /Users/ryan/.nvm/versions/node/v6.7.0/lib/node_modules/angular-cli/node_modules/resolve/lib/async.js:44:21
    at ondir (/Users/ryan/.nvm/versions/node/v6.7.0/lib/node_modules/angular-cli/node_modules/resolve/lib/async.js:187:31)
    at /Users/ryan/.nvm/versions/node/v6.7.0/lib/node_modules/angular-cli/node_modules/resolve/lib/async.js:153:39

I have found lots of answers for similar errors, but they all seem to suggest that the issue was resolved in v1.0.0-beta.15, while I should have installed v1.0.0-beta.16. Regardless, when I follow the most popular suggestion I can find of commenting out line 25 of ng (Version.assertPostWebpackVersion();), I just get a different error:

→ ng --version
module.js:457
    throw err;
    ^

Error: Cannot find module '../models/config'
    at Function.Module._resolveFilename (module.js:455:15)
    at Function.Module._load (module.js:403:25)
    at Module.require (module.js:483:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/Users/ryan/node_modules/angular-cli/packages/angular-cli/addon/index.js:4:16)
    at Module._compile (module.js:556:32)
    at Object.Module._extensions..js (module.js:565:10)
    at Module.load (module.js:473:32)
    at tryModuleLoad (module.js:432:12)
    at Function.Module._load (module.js:424:3)
    at Module.require (module.js:483:17)
    at require (internal/module.js:20:19)
    at Function.Addon.lookup (/Users/ryan/node_modules/ember-cli/lib/models/addon.js:879:19)
    at /Users/ryan/node_modules/ember-cli/lib/models/addons-factory.js:47:36
    at visit (/Users/ryan/node_modules/ember-cli/lib/utilities/DAG.js:23:3)
    at DAG.topsort (/Users/ryan/node_modules/ember-cli/lib/utilities/DAG.js:82:7)
    at AddonsFactory.initializeAddons (/Users/ryan/node_modules/ember-cli/lib/models/addons-factory.js:44:9)
    at Project.initializeAddons (/Users/ryan/node_modules/ember-cli/lib/models/project.js:374:36)
    at Function.Project.nullProject (/Users/ryan/node_modules/ember-cli/lib/models/project.js:131:16)
    at Function.Project.projectOrnullProject (/Users/ryan/node_modules/ember-cli/lib/models/project.js:640:22)
    at module.exports (/Users/ryan/node_modules/ember-cli/lib/cli/index.js:94:25)
    at module.exports (/Users/ryan/node_modules/angular-cli/packages/angular-cli/lib/cli/index.js:45:10)
    at /Users/ryan/.nvm/versions/node/v6.7.0/lib/node_modules/angular-cli/bin/ng:36:5
    at /Users/ryan/.nvm/versions/node/v6.7.0/lib/node_modules/angular-cli/node_modules/resolve/lib/async.js:44:21
    at ondir (/Users/ryan/.nvm/versions/node/v6.7.0/lib/node_modules/angular-cli/node_modules/resolve/lib/async.js:187:31)
    at /Users/ryan/.nvm/versions/node/v6.7.0/lib/node_modules/angular-cli/node_modules/resolve/lib/async.js:153:39
    at onex (/Users/ryan/.nvm/versions/node/v6.7.0/lib/node_modules/angular-cli/node_modules/resolve/lib/async.js:93:22)
    at /Users/ryan/.nvm/versions/node/v6.7.0/lib/node_modules/angular-cli/node_modules/resolve/lib/async.js:24:18

Thanks in advance for the help.

node: v6.7.0

npm: 3.10.3

os: osx El Capitan

EDIT: Additional information about my current setup, as requested by comments in answer submissions:

→ npm list -g typescript
/Users/ryan/.nvm/versions/node/v6.7.0/lib
└─┬ angular-cli@1.0.0-beta.16
  ├─┬ @angular-cli/ast-tools@1.0.2
  │ └── typescript@2.0.0
  ├─┬ typedoc@0.4.5
  │ └── typescript@1.8.10
  └── typescript@2.0.2

→ npm list -g webpack
/Users/ryan/.nvm/versions/node/v6.7.0/lib
└─┬ angular-cli@1.0.0-beta.16
  └── webpack@2.1.0-beta.22

→ npm list -g angular-cli
/Users/ryan/.nvm/versions/node/v6.7.0/lib
└── angular-cli@1.0.0-beta.16

回答1:

Try running ng --version in a directory with no node_modules and package.json, which can affect the result.

Try do the following to update all your global packages:

npm update -g

My OS X setup

> ng --version                                                                                                     
angular-cli: 1.0.0-beta.16                                                                                                                               
node: 6.7.0                                                                                                                                              
os: darwin x64                                                                                                                                           
> node -v                                                                                                          
v6.7.0                                                                                                                                                   
> npm -v                                                                                                           
3.10.8                                                                                                                                                   
> npm list -g typescript                                                                                           
/usr/local/lib                                                                                                                                           
├─┬ angular-cli@1.0.0-beta.16                                                                                                                            
│ ├─┬ @angular-cli/ast-tools@1.0.2                                                                                                                       
│ │ └── typescript@2.0.0                                                                                                                                 
│ ├─┬ typedoc@0.4.5                                                                                                                                      
│ │ └── typescript@1.8.10                                                                                                                                
│ └── typescript@2.0.2                                                                                                                                   
└─┬ typings@1.4.0                                                                                                                                        
  └─┬ typings-core@1.5.0                                                                                                                                 
    └── typescript@2.0.3                                                                                                                                 

> npm list -g webpack                                                                                              
/usr/local/lib                                                                                                                                           
├─┬ angular-cli@1.0.0-beta.16                                                                                                                            
│ └── webpack@2.1.0-beta.22                                                                                                                              
└── webpack@1.13.2                                                                                                                                       

> npm list -g angular-cli                                                                                          
/usr/local/lib                                                                                                                                           
└── angular-cli@1.0.0-beta.16


回答2:

Prerequisites

First, ensure you have Node 4.x and NPM 3.x.

Note that Node 4.x (at least up to 4.4.2, possibly newer versions) comes with NPM 2.x not 3.x.

So, if you have Node 4.x, upgrade NPM to latest by running:

npm i -g npm

(Use sudo if needed; and BTW, run install as shown, not update)

If you have Node 0.10 or 0.12, you probably want to install it, and look in your ~/ directory for any files or folders mentioning node or npm and remove them. Those versions were so sticky.

Clean Install

Now, assuming you have all the right versions, run:

npm rm -g angular-cli
npm cache clear
npm i -g angular-cli

More Troubleshooting

If this doesn't work, repeat the previous 3 lines, but add --no-optional to the install line, so this becomes:

npm rm -g angular-cli
npm cache clear
npm i -g angular-cli --no-optional