Search code examples
angularwebpackangular-cliamd

How to use AMD with angular-cli?


I am trying to move my existing angular 2 project to angular-cli. My current scaffolding uses Webpack and I use AMD quite a lot to load certain js files only inside some components asynchronous. Ex :

require(['json-fn'], (JSONfn) => {
        srvc.JSONfn = JSONfn;
    });

the new angular-cli scaffolding gives the following error :

 Cannot find name 'require'.

and webpack compilation fails. What do I do?


Solution

  • angular-cli uses webpack 2 to build projects, which supports AMD, however you need to use the import statement:

    import * as jsonFn from 'json-fn';
    
    // ..
    
    ngOnInit() {
        // use it normally here
        jsonFn.parse();
    }