I have an exported class in my working Angular2 app using ES6 module:
//File = init.todos.ts
export class Init {
load() {
...
}
}
I'm importing this class from another component via :
//File = todo.service.ts
import { Init } from './init.todos'
It works as expected.
However if I change the loading mechanism to commonjs :
//File = init.todos.ts
export class Init {
load() {
...
}
}
module.exports.Init = Init;
Requiring it:
//File = todo.service.ts
var Init = require("./init.todos");
— I get those 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'.)
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 :

"module": "system". There's no require function in SystemJS.module.exportsbut I still should import it via es6 syntax ?const x = require('blah'), you will be using NodeJS/CommonJS require and "lose" typing sincexwill be mapped to any. This may be useful to import JS librairies which are not typed though