23

I'm trying to use relative path when defining templateUrl in angular2 component, as described here: http://schwarty.com/2015/12/22/angular2-relative-paths-for-templateurl-and-styleurls/

@Component({
    selector: 'login-app',
    moduleId: module.id,
    templateUrl: './app.html'
})

although I keep getting:

ERROR: module is not defined

I am using --m commonjs in TypeScripy compile settings.

How to make it work?

3
  • What module loader do you use? (systemjs? webpack? browserify?) Commented Jan 1, 2016 at 13:24
  • I don't believe your problem has anything to do with relative URLs. Does it work if you put the template in the @Component decorator instead of in a separate file? Commented Jan 1, 2016 at 15:32
  • I use systemjs. @MichaelOryl Everything works well if remove moduleId and use absolute url and not relative, or if I put template code instead of a separate file. Commented Jan 1, 2016 at 15:34

8 Answers 8

13

This simply doesn't seem to work for me. I can see in the Github repo for Angular 2 that the feature was supposedly added in early December, but it doesn't function at all best I can tell.

The docs have not been updated to reflect it, and it's still missing proper tests from what I can tell.

Update:

Here's part of what's going on. This requires a CommonJS specific feature to work, so you have to have that module system in use in your tsconfig.json file.

On top of that, there's an issue when using SystemJS because of the way it bundles everything into the root.

Basically there seem to be a lot of restrictions on being able to use this new feature.

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

3 Comments

I've tried playing with it, like specifying commonjs as the module in typescriptOptions, but still no go. Have you been able to get it working?
@Sagi I have never gotten relative paths to work, I'm afraid.
i'm using commonJs, my HTML/ts files are in app folder but compiled files i.r js files in another dist folder from where my app ran, now when i use moduleId: module.id it took path from 'dist/blabla...' but there angular2 found only .js files not HTML now how to tell angular to take path from src folder instead of dist folder ?
5

You can try some steps as follow:

Step 1. Declares the ‘commonjs’ format module object that identifies the “module id” for the current module.

"compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    ...
}

Step 2: Adding typings.d.ts into wwwroot folder for identifies “module id”.

/// <reference path="../typings/globals/core-js/index.d.ts" />
 declare var module: { id: string };

Step 3: Set a component’s moduleId metadata property to module.id for module-relative urls (please note the convention).

@Component({
    selector: 'login-app',
    moduleId: module.id,
    templateUrl: 'app.component.html'
})

Hope this help.

Comments

4

A workaround : use require with template instead of templateUrl

@Component({
    selector: 'login-app',
    template: require('./app.html')
})

6 Comments

Tried this, however I then get Error: ReferenceError: require is not defined in: angular2-polyfills.js
interesting idea, however this means that require has to load a string instead of javascript, which i don't think it does by default. maybe this works with bennadel.com/blog/…, but that seems like too much work for this.
It works for me with webpack. I didn't try with systemjs.
@domen how do you import require on typescript?
no import for require. I use angular2-webpack-starter. all works fine
|
3

I had the same problem:

error TS2304: Cannot find name 'module'.

for some reasons even with "module": "commonjs" in compilerOptions in tsconfig.json file TypeScript rise this error. So I solved the problem by adding declaration for it:

declare var module: {
    id: string;
};

9 Comments

where to add these lines in the tsConfig.json file ?
@PardeepJain, I created another file for this. You can take a look on example here
i am trying to assign value to id but angular will not allow why ? i want this stackoverflow.com/q/36451917/5043867
@PardeepJain, in this case you could replace path: moduleId: module.id.replace("/dist/", "/src/"), or create gulp/grunt task that will copy all html files from src to dist
@PardeepJain, as note above you could use additional gulp task that will copy css to output folder
|
1

Just set the "moduleId" property of the Component with module.id value.

Please refer to the example here: https://angular.io/docs/ts/latest/cookbook/component-relative-paths.html#!#set-the-moduleid-

Sample code:

@Component({
    moduleId: module.id,
    selector: 'login',
    templateUrl: "login.component.html"
})

2 Comments

yeah, with just the 'starter' as template and this advice, i managed to get it working
its weird, you would expect to have to define this 'module' object somewhere or expect to see it as part of the official tutorial, but here we are..
0

With systemjs, first declare the string __moduleName (two underscores) and then use it instead of module.id as follows:

declare var __moduleName: string;

@Component({
    selector: 'login-app',
    moduleId: __moduleName,
    templateUrl: './app.html'
})

Comments

-1

module is actually defined inside index.d.ts in node typing.

Add node in your typings.json as shown below and run 'npm run typings install'.

{
  "globalDependencies": {
    "core-js": "registry:dt/core-js#0.0.0+20160602141332",
    "jasmine": "registry:dt/jasmine#2.2.0+20160621224255",
    "node": "registry:dt/node#6.0.0+20160621231320"
  }
}

This should resolve your issue.

Comments

-2

Use absolute path of html file when using temlplateUrl. for example: If your application hierarchy is MyApp->app->yourComponent.ts and at the app level your app.html is there then address the Component decorator of yourComponent.ts as follows, it should work.

@Component({ selector: 'login-app', templateUrl: 'app/app.html' })

relative path never worked for me, even after setting moduleID to module.id or any other work around. If it does work for anyone please do share.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.