0

I'm trying to follow these instructions, and after I do the command prompt steps, it tells me to import and create an instance of the SparkPost class:

const SparkPost = require(‘sparkpost’)
const client = new SparkPost('YOUR API KEY')

But I have no idea where they want me to do that. Certainly not command prompt. There are dozens of files in the module they had me make, so I'm lost. Please help.

I've also looked here, but that left me just as confused. It's like they think I should already know.

UPDATE: I saved this code as index.js. here's a screenshot of my file structure

/*this is my code from the sparkpost tutorial*/
const SparkPost = require(‘sparkpost’);
const client = new SparkPost('XXX-my-api-XXX');

/*this is my code from the node.js tutorial mentioned in the comments*/
const http = require('http');

const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World\n');
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

and here's what it looks like when I try to launch:

C:\Users\jacob\sparkpost-test>node index.js
C:\Users\jacob\sparkpost-test\index.js:1
(function (exports, require, module, __filename, __dirname) { const SparkPost = require(‘sparkpost’);


SyntaxError: Invalid or unexpected token
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Module._compile (module.js:607:28)
    at Object.Module._extensions..js (module.js:654:10)
    at Module.load (module.js:556:32)
    at tryModuleLoad (module.js:499:12)
    at Function.Module._load (module.js:491:3)
    at Function.Module.runMain (module.js:684:10)
    at startup (bootstrap_node.js:187:16)
    at bootstrap_node.js:608:3
Dawn
  • 19
  • 5
  • You have to declare the const SparkPost = require('sparkpost'); const client = new SparkPost('YOUR_API_KEY'); in the js file you are working to send the email normally you can set it at the top of the file – Angel Feb 07 '18 at 18:05
  • Those lines should go in the javascript file you'll run to execute your project's code. [This tutorial](https://nodejs.org/en/docs/guides/getting-started-guide/) might help you get started – Dave Gray Feb 07 '18 at 18:06
  • Thank you @DaveGray, I was able to get that tutorial to work. However, when I added the code from sparkpost to the top of the file, and then tried to run it in the terminal, I got this error: (function (exports, require, module, __filename, __dirname) { const SparkPost = require(‘sparkpost’); SyntaxError: Invalid or unexpected token at createScript (vm.js:80:10) at Object.runInThisContext (vm.js:139:10) at Module._compile (module.js:607:28) at Object.Module._extensions..js (module.js:654:10) at Module.load (module.js:556:32) etc. . . – Dawn Feb 07 '18 at 21:53
  • Hi @Dawn, could you please update the question with your code so we can try it out? – Dave Gray Feb 08 '18 at 15:46

0 Answers0