npm post-install typings not working correctly

2019-04-06 00:53发布

问题:

I have the following packages.json:

{
"name": "shopping-assistant-angular",
"version": "1.0.0",
"scripts": {
    "start": "concurrent \"npm run tsc:w\" \"npm run lite\" ",    
    "tsc": "tsc",
    "tsc:w": "tsc -w",
    "lite": "lite-server",
    "typings": "typings",
    "postinstall": "typings install" 
},
"license": "ISC",
"dependencies": {
    "angular2": "2.0.0-beta.7",
    "systemjs": "0.19.23",
    "es6-promise": "^3.0.2",
    "es6-shim": "^0.33.3",
    "reflect-metadata": "0.1.2",
    "rxjs": "5.0.0-beta.2",
    "zone.js": "0.5.14"
},
"devDependencies": {
    "bower": "^1.7.7",
    "concurrently": "^1.0.0",
    "grunt": "^0.4.5",
    "grunt-chrome-manifest": "^0.3.0",
    "grunt-contrib-clean": "^1.0.0",
    "grunt-contrib-concat": "^0.5.1",
    "grunt-contrib-copy": "^0.8.2",
    "grunt-contrib-cssmin": "^0.14.0",
    "grunt-contrib-htmlmin": "^0.6.0",
    "grunt-contrib-uglify": "^0.11.1",
    "grunt-filerev": "^2.3.1",
    "grunt-ts": "^5.3.2",
    "grunt-usemin": "^3.1.1",
    "lite-server": "^2.0.1",
    "load-grunt-tasks": "^3.4.0",
    "typescript": "1.7.5",
    "typings": "^0.6.8"
}
}

when I run npm install in my directory I get an error: 'typings' is not recognized as an internal or external command, operable program or batch file.

In the Angular 2 quick-start guide they use the following packages.json:

{
"name": "angular2-quickstart",
"version": "1.0.0",
"scripts": {
    "start": "concurrent \"npm run tsc:w\" \"npm run lite\" ",    
    "tsc": "tsc",
    "tsc:w": "tsc -w",
    "lite": "lite-server",
    "typings": "typings",
    "postinstall": "typings install" 
},
"license": "ISC",
"dependencies": {
    "angular2": "2.0.0-beta.7",
    "systemjs": "0.19.22",
    "es6-promise": "^3.0.2",
    "es6-shim": "^0.33.3",
    "reflect-metadata": "0.1.2",
    "rxjs": "5.0.0-beta.2",
    "zone.js": "0.5.15"
},
"devDependencies": {
    "concurrently": "^2.0.0",
    "lite-server": "^2.1.0",
    "typescript": "^1.7.5",
    "typings":"^0.6.8"
}
}

Which successfully runs "typings install" after all of the packages are done install.

Not sure why running these very similar packages.json files are resulting in completely different outcomes.

回答1:

Gabe O'Leary is right (see comment), first try to install typings globally npm install typings -g if you are using a mac you might need to use the sudo command sudo npm install typings -g



回答2:

I was facing the same issue. You need to add typings.json file in your project folder. Please check this link: https://angular.io/guide/quickstart



回答3:

Oh Em Gee. This tooling is SO fragile!

PowerShell ->
  OS %PATH% spec ->
    Node.js bin folder (which one?) ->
      typings.cmd ->
        Tools : Options : Projects and Solutions : External Web Tools ->
          node.js bin ->
            Typings JavaScript files ->
              PowerShell working directory (vs. project path) ->
                typings.json configuration ->

Note that the npm install typings -g is meant to be a "global" install, so even if you want it to be part of a single project (e.g. you want to be able to check out and build a single project), TOO BAD! You must have the solution checked out, with a solution-wide package.json file, to get your project toolchain.

Note that the default Typings installation modifies a Node.js binaries folder. So if you have multiple Node.js installs (you probably have at least three, one installed with VS update 2, at least one installed using NuGet, and one or more installed as a separate Node.js install to overcome defects in the older versions available from the other two sources), your pain has already begun.

Add to that the fact that some of your development team modified their External Web Tools directory per various MS support articles to address this, and some members of your team have different Node.js versions installed, etc. Now your pain is getting real...

So, what's the solution? Me personally, I'm going to plug "typings install" into my gulp.js "run on project startup" event, and set it to target a project-specific "typings" package. Now we can check out and build a single project, and no one on the team needs to be able to run "typings install" at a command line or package manager console, except the one person responsible for maintaining it.

These paths may also be helpful to you. At different points during upgrades on the machine I did most of this diagnosis on, npm install typings -g placed a typings.cmd file in different directories:

  • %SolutionFolder%\packages\Node.js.5.3.0\typings.cmd
  • C:\Users\%username%\AppData\Roaming\npm\typings.cmd


回答4:

You should not need to install typings globally for the above setup to work. When you run npm install, a symlink to the typings binary should be added at /node_modules/.bin/typings, and the postinstall script should be run with /node_modules/.bin added to the PATH environment variable.

Looking at your setup, though, if you were to install with npm install --production (or with the NODE_ENV environment variable set to production), typings would not be installed as it is in devDependencies, so you would then get the error. This might be the real cause of your problem. Take a look at this question for some suggestions on how to fix this.



回答5:

I tend to have nested angular 1 and angular 2 solutions that I end up storing in Git repos...

When I went to run this command for typing not showing up

npm run typings install

That did NOT work. Then I found this stackoverflow page and tried

npm install typings -g

For me the problem was that at a JOB, I had inherited a .typingsrc file in which even though it was NOT in the folder that I was running npm install etc.. in , it must have decided to look a directory or 2 above and thus no wonder I got the errors.

I simply renamed it to old.typingsrc , as inside the purpose was to get around a proxy proxy="http://proxy-chain.company.com:911"

This might not help very many people, but I think it may help others that DO HAVE a proxy!



回答6:

In ionic 2 , I faced same issue on window 10 then I tried npm install typings -g , after installation I tried typings command and wow it worked, I was working with ionic 2 with socket.io and I needed to run
typings install dt~socket.io-client --save --global , now it is working.

Hope this will help you.