Angular2 Tutorial (Tour of Heroes): Cannot find module 'angular2-in-memory-web-api'

前端 未结 29 2166
青春惊慌失措
青春惊慌失措 2020-11-29 06:25

I have followed the Tutorial. After changing app/maint.ts in the Http chapter I get the error when starting the app via command line:

ap

相关标签:
29条回答
  • 2020-11-29 06:37

    If using angular cli to build your angular2 app, including the angular2-in-memory-web-api involves three steps:

    1. add the api to the system-config.ts file (inside the src subdirectory) like below:

      /** Map relative paths to URLs. */
      const map: any = {
        'angular2-in-memory-web-api': 'vendor/angular2-in-memory-web-api'
      };
      /** User packages configuration. */
      const packages: any = {
        'angular2-in-memory-web-api': { main: 'index.js', defaultExtension: 'js' }
      };
      
    2. Add

      angular2-in-memory-web-api/**/*.+(js|js.map)' 
      

    to the vendorNpmFiles array in the angular-cli-build.js file like ofShard mentioned;

    1. Of course, add to the package.json as described by traneHead; for 2.0.0-rc.3 I use the version below:

      "angular2-in-memory-web-api": "0.0.13"
      

    I find this link "Adding material2 to your project" explains the process of adding third-party libraries quite clearly.

    0 讨论(0)
  • 2020-11-29 06:40

    The main answer helped me: change

    'angular2-in-memory-web-api': { defaultExtension: 'js' },
    

    to

    'angular2-in-memory-web-api': { main: 'index.js', defaultExtension: 'js' }
    
    0 讨论(0)
  • 2020-11-29 06:44

    Here is what worked for me:

    I noticed that the package.json had the following version:

    "angular2-in-memory-web-api": "0.0.20"

    Note the "2" in the name.

    However when referencing InMemoryWebApiModule it was using 'angular-in-memory-web-api' without the 2 in the name. So I added it and it resolved the issue:

    import { InMemoryWebApiModule } from 'angular2-in-memory-web-api';

    Note: I found this in notice section of https://github.com/angular/in-memory-web-api

    As of v.0.1.0, the npm package was renamed from angular2-in-memory-web-api to its current name, angular-in-memory-web-api. All versions after 0.0.21 are shipped under this name. Be sure to update your package.json and import statements.

    0 讨论(0)
  • 2020-11-29 06:44

    Angular 4 Changes

    As of October 17, 2017, the tutorial fails to mention that angular-in-memory-web-api is not included in a standard CLI build, and must be installed separately. This can be easily done with npm:

    $ npm install angular-in-memory-web-api --save

    This automatically handles the necessary changes to package.json, so you don't need to make edits yourself.

    Points of confusion

    This thread is quite confusing as the Angular CLI has changed significantly since this thread was started; a problem with many rapidly-evolving APIs.

    • angular-in-memory-web-api has been renamed; it drops the 2 from angular2 to simply angular
    • The Angular CLI no longer uses SystemJS (replaced by Webpack), so systemjs.config.js no longer exists.
    • npm's package.json should not be edited directly; use the CLI.

    Solution

    As of October 2017, the best fix is to simply install it yourself using npm, as I mentioned above:

    $ npm install angular-in-memory-web-api --save

    Good luck out there!

    0 讨论(0)
  • 2020-11-29 06:46

    The simplest solution I could think of was to install the package with npm and restart the server:

    npm install angular-in-memory-web-api --save
    

    I almost installed the angular2-in-memory-web-api package, but the npm page says:

    All versions after 0.0.21 are (or soon will be) shipped under angular-in-memory-web-api.

    Note: This solution worked for a project that was created with the CLI tools, which does not list the package as a dependency, and might not solve the problem for projects that were created with the Quickstart seed, which does list the package as a dependency.

    0 讨论(0)
  • 2020-11-29 06:47

    The Best way is to install it using NPM e.g

    npm install git+https://github.com/itryan/in-memory-web-api/ --save

    it will add required reference

    0 讨论(0)
提交回复
热议问题