2

When executing my code, a typescript error appears that "require" is not a function, which is why I declared the function beforehand, but now typescript complains that "Modifiers cannot appear here.". Can someone help me?

import { Injectable, ValueProvider } from '@angular/core';

@Injectable({
  providedIn: 'root'
})
export class DataService {
  edge : any;

  constructor() {
    declare function require(name: string); // "Modifiers cannot appear here."
    this.edge = require('edge');
  }

  getData(type: string, method: string) {
    var data = this.edge.func({
      assemblyFile: 'C:\Program Files\Common Files\Siemens\Automation\Simatic OAM\bin\CC.CommonInterfaces.dll',
      typeName: `CC.CommonInterfaces.${type}`,
      methodName: `${method}`
    });

    return data(null, function(error, result) {
      if (error) throw error;
      console.log(result);
      return result;
    });
  }

}
4
  • Could you please provide a minimal working example? Maybe in a StackBlitz? Also, what exactly are you trying to do? Is edge a dependency that you want to import? Commented Jan 23, 2020 at 9:59
  • @Wernerson don't know how to show a minimal working example of this. But I can explain what I'm doing here. I'm basically calling C# functions from a .dll in my DataService and want to use the data (from getData()) in my Angular Project. To call C# functions I need edge.js, which needs to be called with a require() function which typescript does not know about until runtime, which is why I declare the function beforehand but it does not work. Commented Jan 23, 2020 at 10:04
  • Why are you even using require? You should be importing... Otherwise, use declare in .d.ts files. Commented Jan 23, 2020 at 10:07
  • It seems to me like edge is a dependency and thus, needs to be imported just like you did with Injectable and ValueProvider form @angular/core. Checkout Rahuls answer. Commented Jan 23, 2020 at 10:08

1 Answer 1

3

You can directly import edge instead of using require:

import { Injectable, ValueProvider } from "@angular/core";

import * as edge from "edge";

@Injectable({
  providedIn: "root"
})
export class DataService {
  constructor() {}

  getData(type: string, method: string) {
    let data = edge.func({
      assemblyFile:
        "C:Program FilesCommon FilesSiemensAutomationSimatic OAM\binCC.CommonInterfaces.dll",
      typeName: `CC.CommonInterfaces.${type}`,
      methodName: `${method}`
    });

    return data(null, function(error, result) {
      if (error) throw error;
      console.log(result);
      return result;
    });
  }
}
Sign up to request clarification or add additional context in comments.

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.