Vonage / vonage-node-sdk

Vonage API client for Node.js. API support for SMS, Voice, Text-to-Speech, Numbers, Verify (2FA) and more.
Apache License 2.0
380 stars 181 forks source link

ReferenceError: Nexmo is not defined #72

Closed derhyl closed 7 years ago

derhyl commented 7 years ago

Hi, It is impossible to instantiate Nexmo. I always an error : ReferenceError: Nexmo is not defined, trigerred on /node_modules/nexmo/lib/Nexmo.js:76:15. Of course, I did npm install nexmo before... Can you please help on that? Thanks a lot,

leggetter commented 7 years ago

Hi @lulucious,

Yes, If you have installed v1.0.0 then the usage is:

var Nexmo = require('nexmo');

var nexmo = new Nexmo(...);

These quick start examples should help: https://github.com/nexmo-community/nexmo-node-quickstart

If they don't, could you please provide some example code? Or a repo that demonstrates the problem would be great too.

Thanks.

derhyl commented 7 years ago

well this is EXACTLY what I did... Do you confirm that : applicationId, privateKey and options are not mandatory? Are you sure npm install nexmo is pointing to the right release?

By the way there is an extra } in your readme file : var nexmo = new Nexmo({ apiKey: API_KEY, apiSecret: API_SECRET, applicationId: APP_ID, privateKey: PRIVATE_KEY_PATH, }, options });

leggetter commented 7 years ago

Do you confirm that : applicationId, privateKey and options are not mandatory?

Yes, I can confirm that applicationId and privateKey are not mandatory.

Are you sure npm install nexmo is pointing to the right release?

https://www.npmjs.com/package/nexmo

Is correctly pointing to 1.0.0.

If you run:

git clone git@github.com:nexmo-community/nexmo-node-quickstart.git
cd nexmo-node-quickstart
mv .env-example .env

And populate the values in the .env with the EXAMPLE_FROM_NUMBER being a virtual number you are renting from Nexmo and EXAMPLE_TO_NUMBER your number. Then run:

node sms/send.js

You phone should receive an SMS. I've just tested this.

If you look at sms/send.js you'll see it uses the constructor without the applicationId and privateKey:

var Nexmo = require('nexmo');

var nexmo = new Nexmo({apiKey: API_KEY, apiSecret: API_SECRET}, {debug:true});
nexmo.message.sendSms(FROM_NUMBER, TO_NUMBER, 'Hello from Nexmo!');

I hope this helps.

derhyl commented 7 years ago

Ok I found the problem, but I can't explain it easily... the presence of this line in the file that requires nexmo breaks my call to the module : body.match(/â|ê|î|ô|û|ë|ï|ç|ù/g)? then 'unicode' else 'text' Any idea?

leggetter commented 7 years ago

@lulucious can you provide any more context? Could you provide a code sample that reproduces the problem?

derhyl commented 7 years ago

If a special caracter, or a single quote () is in your js file (even in a commented line!!), Node cannot compile the nexmo module...

cbetta commented 7 years ago

@lulucious I'm not sure this is the issue. See https://github.com/cbetta/node-nexmo-unicode-example for proof that this works fine.

Did you actually put nexmo in your package.json?

derhyl commented 7 years ago

yes of course! I really did a lot of investigation before finding the guilty lines... This only worked when I removed that line and the one with the slanted quote. Even commenting the line did not help. Only removing it did. I can't explain and i agree that it sounds unrelated but it is the case...

derhyl commented 7 years ago

Just add on line 2 :

nexmo = new Nexmo({ apiKey: "yyy", apiSecret: "xxx" }, { debug: true });

cbetta commented 7 years ago

@lulucious still works for me https://github.com/cbetta/node-nexmo-unicode-example (updated with your code)

cbetta commented 7 years ago

@lulucious you have a typo on line 1, it needs to be var Nexmo with a capital N

derhyl commented 7 years ago

Hmm.. it is working when i run it in js but not with coffeescript

This is ok :

Nexmo = require('nexmo');

nexmo = new Nexmo({
  apiKey: "ii",
  apiSecret: "hh"
}, {
  debug: true
});

body = "";

body = body.replace(/’/g, "'");

console.log("ok");

But this is not :

Nexmo = require('nexmo')
nexmo = new Nexmo({apiKey:"ii", apiSecret:"hh"}, {debug:true})

body = ""

body = body.replace(/’/g,"'")

console.log "ok"

However, it only triggers a problem with nexmo module! weird...

cbetta commented 7 years ago

@lulucious both of them work fine here in js and coffee. I'm 99% certain your error is just referring to the first line in the code due to a syntax error (incorrectly). I'm pretty sure it's not our library in this case. I'd like to close this issue until we can actually prove this to be an issue with our library because I can't see how it is.

You ok with that?

cbetta commented 7 years ago

@lulucious FYI I ran this code below and it works fine here.

Nexmo = require('nexmo')
nexmo = new Nexmo({apiKey:"ii", apiSecret:"hh"}, {debug:true})
body = ""
body = body.replace(/’/g,"'")
console.log "ok"
cbetta commented 7 years ago

@lulucious just to be sure: what NPM/Node versions are you running?

derhyl commented 7 years ago

v4.4.3

derhyl commented 7 years ago

that same code returns :

Nexmo = require('nexmo')
nexmo = new Nexmo({apiKey:"ii", apiSecret:"hh"}, {debug:true})
body = ""
body = body.replace(/’/g,"'")
console.log "ok"
ReferenceError: Nexmo is not defined
  at new Nexmo (/home/ldmint/Public/node-nexmo-unicode-example-master/node_modules/nexmo/lib/Nexmo.js:77:25)
  at Object.<anonymous> (/home/ldmint/Public/node-nexmo-unicode-example-master/app.coffee:2:13)
  at Object.<anonymous> (/home/ldmint/Public/node-nexmo-unicode-example-master/app.coffee:1:1)
  at Module._compile (module.js:460:26)
  at Object.exports.run (/home/ldmint/.nvm/versions/node/v0.12.7/lib/node_modules/coffee-script/lib/coffee-script/coffee-script.js:134:23)
  at compileScript (/home/ldmint/.nvm/versions/node/v0.12.7/lib/node_modules/coffee-script/lib/coffee-script/command.js:224:29)
  at compilePath (/home/ldmint/.nvm/versions/node/v0.12.7/lib/node_modules/coffee-script/lib/coffee-script/command.js:174:14)
  at Object.exports.run (/home/ldmint/.nvm/versions/node/v0.12.7/lib/node_modules/coffee-script/lib/coffee-script/command.js:98:20)
  at Object.<anonymous> (/home/ldmint/.nvm/versions/node/v0.12.7/lib/node_modules/coffee-script/bin/coffee:7:41)
  at Module._compile (module.js:460:26)
  at Object.Module._extensions..js (module.js:478:10)
  at Module.load (module.js:355:32)
  at Function.Module._load (module.js:310:12)
  at Function.Module.runMain (module.js:501:10)
  at startup (node.js:129:16)
  at node.js:814:3
cbetta commented 7 years ago

@lulucious can you make me a project with package.json and everything like I did with https://github.com/cbetta/node-nexmo-unicode-example that shows it not working?

cbetta commented 7 years ago

@lulucious tested in Node 4.4.3 and it all works fine here. Both coffeescript and Node.

cbetta commented 7 years ago

FYI I am running CoffeeScript version 1.11.1

derhyl commented 7 years ago

try running coffee app.coffee https://github.com/lulucious/nexmo-weird-pb

cbetta commented 7 years ago

@lulucious I'll get back to this later tonight.

cbetta commented 7 years ago

@lulucious I tried your code and it all still works pretty fine for me. Both the Node and the Coffee version. There's something on your system definitely wrong.

cbetta commented 7 years ago

@lulucious could you try a different Node version on your machine for me? Just want to see if that makes it go away.

cbetta commented 7 years ago

Closing due to inactvity