The Controller should be found.
An error is thrown (in stack trace at the bottom). Short Version Cannot find name Controller
Currently I don麓t have one.
import { injectable, interfaces, Kernel } from 'inversify';
import { Controller, InversifyExpressServer, TYPE } from 'inversify-express-utils';
const kernel: interfaces.Kernel = new Kernel();
kernel.bind<Controller>(TYPE.Controller).to(MyController).whenTargetNamed('MyController');
@injectable()
@Controller('/api')
class MyController {
constructor() {
console.log('do something');
}
}
The error is thrown in line 5. But in line 2 the Controller is imported so the error should not be thrown.
The code is started with ts-node test.ts, but trying to compile it with tsc test.ts the same error is thrown.
src/master/api/api.ts (15,17): Cannot find name 'Controller'. (2304)
at getOutput (/home/me/.nvm/versions/node/v6.3.1/lib/node_modules/ts-node/src/index.ts:258:17)
at /home/me/.nvm/versions/node/v6.3.1/lib/node_modules/ts-node/src/index.ts:267:16
at Object.compile (/home/me/.nvm/versions/node/v6.3.1/lib/node_modules/ts-node/src/index.ts:403:17)
at loader (/home/me/.nvm/versions/node/v6.3.1/lib/node_modules/ts-node/src/index.ts:289:33)
at Object.require.extensions.(anonymous function) [as .ts] (/home/me/.nvm/versions/node/v6.3.1/lib/node_modules/ts-node/src/index.ts:306:14)
at Module.load (module.js:458:32)
at tryModuleLoad (module.js:417:12)
at Function.Module._load (module.js:409:3)
at Module.require (module.js:468:17)
at require (internal/module.js:20:19)src/master/api/api.ts (15,17): Cannot find name 'Controller'. (2304)
at getOutput (/home/me/.nvm/versions/node/v6.3.1/lib/node_modules/ts-node/src/index.ts:258:17)
at /home/me/.nvm/versions/node/v6.3.1/lib/node_modules/ts-node/src/index.ts:267:16
at Object.compile (/home/me/.nvm/versions/node/v6.3.1/lib/node_modules/ts-node/src/index.ts:403:17)
at loader (/home/me/.nvm/versions/node/v6.3.1/lib/node_modules/ts-node/src/index.ts:289:33)
at Object.require.extensions.(anonymous function) [as .ts] (/home/me/.nvm/versions/node/v6.3.1/lib/node_modules/ts-node/src/index.ts:306:14)
at Module.load (module.js:458:32)
at tryModuleLoad (module.js:417:12)
at Function.Module._load (module.js:409:3)
at Module.require (module.js:468:17)
at require (internal/module.js:20:19)
Hi @lholznagel this is working in the express examples right? If so it must be something in your config.
Yep, but I also had the same problem there, before I did "something". I thought it was because of the migration but now the same problem occurs again, which is why I created this issue as a "reminder" and to find the problem so that we can add some information to "How t use it" topic.
Will resume searching for the problem tomorrow.
No problem thanks for reporting it 馃憤
Yeap, same here. Shouldn't decorators.d.ts export the Controller interface that is importing from interfaces.d.ts?
export declare function Controller(path: string, ...middleware: express.RequestHandler[]): (target: any) => void;
the function under decorators.d.ts is exported.
In the file interfaces.d.ts the interface is exported:
import * as express from "express";
declare namespace interfaces {
...
interface Controller {
}
...
}
export default interfaces;
But you are exporting the @Controller decorator. How about the Controller interface?
Note that I'm not getting an error when using the @Controller decorator, but rather when I try to use the interface like in the example:
@Controller('/foo')
@injectable()
export class FooController implements Controller { // I get a "Controller is undefined" here
}
(I might be missing something here, new to TS)
Okay found it. I added the namespace interface to the exported stuff:
import { InversifyExpressServer } from "./server";
import { Controller, Method, Get, Put, Post, Patch, Head, All, Delete } from "./decorators";
import { TYPE } from "./constants";
import { interfaces } from './interfaces';
export { InversifyExpressServer, Controller, Method, Get, Put, Post, Patch, Head, All, Delete, TYPE };
export { interfaces };
in the code I now use:
import { interfaces } from 'inversifyexpress-utils';
const kernel = new Kernel();
kernel.bind<interfaces.Controll>()...
@luisfarzati the implements Controller can be removed, works for me.
@remojansen you sir, are awesome! The latest release seems to fix it. Removing node_modules and reinstalling everything works.
Thanks! 馃槃 I will document here the fix...
To fix this problem you need to use [email protected] then you can import both interfaces and Controller decorator:
import { injectable, interfaces as inversifyInterfaces, Kernel } from 'inversify';
import { interfaces as expressUtilsInterfaces, Controller, InversifyExpressServer, TYPE } from 'inversify-express-utils';
const kernel: inversifyInterfaces.Kernel = new Kernel();
kernel.bind<expressUtilsInterfaces.Controller>(TYPE.Controller).to(MyController).whenTargetNamed('MyController');
@injectable()
@Controller('/api')
class MyController implements expressUtilsInterfaces.Controller {
constructor() {
console.log('do something');
}
}
Hi @luisfarzati can you please confirm that this issue has been solved for you? Thanks
@remojansen thanks! Working now. Ideally, would be nice to be able to import Controller as is, but that's just me being too picky :D Thank you!
Great, I will close this issue now.
Just to clarify Controller (decorator) and Controller (interface) are not the same thing:
import { interfaces, Controller } from 'inversify-express-utils';
@Controller('/api') // Decorator
class MyController implements interfaces.Controller { // Interface
// ...
}
That's why it is not possible to have one unique import.
@remojansen This works perfectly when I'm not using uglifyjs with manging enabled. When I am using uglifyjs with mangling enabled, i'm getting this error: Error: No matching bindings found for serviceIdentifier: Symbol(Controller).
container.bind<invInterfaces.Controller>(TYPE.Controller).to(controllers.AuthController).whenTargetNamed("AuthController");
@Controller("/auth")
@injectable()
export class AuthController implements interfaces.Controller {
constructor( @inject(TYPES.AuthService) private service: IAuthService) { }
inversify version 3.0.0-rc.4
inversify-express-utils: 3.0.0-beta.1
Any idea what that could be? Maybe this is the wrong place to post this, but I'm not sure if it's worth opening a new issue.
From the docs:
The compressor might drop unused variables / unreachable code and this might change the number of identifiers or their position.
This is a problem because some variables may not be used at design time and be used only at run-time. I'm not sure about which variable its being dropped. I will have to do some testing to figure it out. If you could provide minimal source code that reproduces the issue It will facilitate future investigation.
Most helpful comment
Thanks! 馃槃 I will document here the fix...
To fix this problem you need to use
[email protected]then you can import both interfaces and Controller decorator: