In my Angular2 app, I have a class
exported using ES6 modules:
//File = init.todos.ts
export class Init {
load() {
...
}
}
I am importing this class into another component like this:
//File = todo.service.ts
import { Init } from './init.todos'
Everything works as expected.
However, when I change the loading mechanism to commonjs:
//File = init.todos.ts
export class Init {
load() {
...
}
}
module.exports.Init = Init;
When trying to require it:
//File = todo.service.ts
var Init = require("./init.todos");
I encounter these errors:
...myApp/src/app/todo.service.ts (4,13): Cannot find name 'require'.) ...myApp/src/app/todo.service.ts (12,14): Property 'load' does not exist on type 'TodoService'.)
https://i.sstatic.net/akUPR.png
Question:
How can I also load commonjs modules using require
?
Tsconfig.json:
{
"compileOnSave": false,
"compilerOptions": {
"outDir": "./dist/out-tsc",
"baseUrl": "src",
"module": "system",
"sourceMap": true,
"declaration": false,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es5",
"typeRoots": [
"node_modules/@types"
],
"lib": [
"es2016",
"dom"
]
}
}
Here are the config files :