Search code examples
angularjsnode.jsionic-frameworkrequire

Uncaught error "require is not defined" in Ionic 1


I'm working in an Ionic 1 project. What i'm trying to make is a function that sends a notification using the package ionic-push-server. But for some reason the program trows me an error saying that the require function is not defined.

ReferenceError: require is not defined

This is my code (taken from https://www.npmjs.com/package/ionic-push-server)

$scope.notification = function(){
        var ionicPushServer = require('ionic-push-server');

        var credentials = {
            IonicApplicationID : "APP_ID",
            IonicApplicationAPItoken : "APP_API_TOKEN"
        };

        var notification = {
            "tokens": TOKEN,
            "profile": "push",
            "notification": {
                "title": "Hola",
                "message": "Hola mundo!"
            } 
        };
        ionicPushServer(credentials, notification);
    };

I've already installed the package using npm, so i know this is not the problem.

I've tried to do something like this:

<script src="node_modules/ionic-push-server/server.js"></script>

And use the function inside (export), but it trowed me an error like this:

 GET http://localhost:8100/node_modules/ionic-push-server/server.js 

Also i've tried doing this:

import * as ionicPushServer from "ionic-push-server";

But i've got this error:

Uncaught SyntaxError: Unexpected token import


Solution

  • I could solve it using local notifications.

    https://github.com/katzer/cordova-plugin-local-notifications