2

I'm trying angular 2 with TS and SystemJS. I would like to map one of my folders in system.config.js.

|_ app
|   |_ standalone
|   |  |_ rest.service.js
|   |  |_ rest.service.js.map
|   |  |_ rest.service.ts
|   |_ app.module.ts
|_ node_modules
|_ system.config.js

This is a simplified version of what I have in system.config.js:

System.config({
        paths: {
            'npm:': 'node_modules/'
        },
        map: {           
            '@angular/core': 'npm:@angular/core/bundles/core.umd.js',
            'rxjs': 'npm:rxjs',
            'stalone': 'app/standalone'
        },
        packages: {
            app: {
                main: './main.js',
                defaultExtension: 'js'
            },
            stalone: {
                defaultExtension: 'js'
            },
            rxjs: {
                defaultExtension: 'js'
            }
        }
    });
})(this);

Two of my imports in app.module.ts are:

import { RestService } from "stalone/rest.service";
import { NgModule }    from "@angular/core";

"@angular.core" does get imported without problems, but when importing "stalone/rest.service", I get the error:

Cannot find module 'stalone/rest.service'.

I've only included files, that I think are relevant in this matter. If I've missed any, please let me know.

3
  • I'm having the same problem, no luck yet. I'll let you know if I find anything useful! Commented Oct 27, 2016 at 7:33
  • typescriptlang.org/docs/handbook/module-resolution.html Commented Nov 1, 2016 at 6:55
  • I'm not sure how the mapping would help as you are under app you can import into app.module.ts with import { RestService } from "./standalone/rest.service". SystemJS will be expecting a module. Commented Nov 2, 2016 at 5:44

3 Answers 3

1

Typescript is not using system.js during compilation. If I understand correctly, you get this error during compilation, so that really means that TypeScript cannot find your module, not system.js that will do that in runtime.

It really works with angular because typescript is looking for files under all node_modules folders where it finds it, but this does not work for you, since your module is not under node_modules.

That's why you need to tell the ts compiler where to find your modules, something like this:

{
  "compilerOptions": {
    "paths": {
      "stalone/*": ["app/standalone/*"]
    }
}

To understand how all this works in more details, you should really check the module resolution documentation for typescript here.

Sign up to request clarification or add additional context in comments.

Comments

0

If you get this error during typescript compilation, then add:

"compilerOptions": {
   ...
    "paths": {
        "standalone/*": [ "app/standalone/*" ]
    }
}

to your tsconfig.json

Note: you need TypeScript version 2.0 or higher.

Comments

0

Try Like this 'stalone': './app/standalone'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.