Beginner issue - node.js - contentful const not recognized

Hello,

I can’t get the contentful connection up and running and this is what I have:

I have a node script that makes the connection to contentful named connectcontentful.js in the data folder with this code:


import * as contentful from 'contentful'

const SPACE_ID = '123456'
const ACCESS_TOKEN = 'ABCGDEFG'

const contentclient = contentful.createClient({
    space: SPACE_ID,
    accessToken: ACCESS_TOKEN
})
export default contentclient

Then I want to load that into my app.js with this code:


var cc = require('./data/connectcontentful');

cc.contentclient.getEntry('3dImb5wbU7dD1qdllUgBY6')
.then(function (entry) {
  // logs the entry metadata
  console.log(entry.sys)
  // logs all fields
  console.log(entry.fields)
  // logs the field with ID image
  console.log(entry.fields.image)
})

When I run app.js I get this error:


TypeError: Cannot read property 'getEntry' of undefined

./src/App.js

src/App.js:3

```
  1 | var cc = require('./data/connectcontentful');  2 | > 3 | cc.contentclient.getEntry('3dImb5wbU7dD1qdllUgBY6')  4 | .then(function (entry) {  5 |   // logs the entry metadata  6 |   console.log(entry.sys)
```

View compiled

__webpack_require__

/Users/timloenders/Dropbox/co-own.it/app2/webpack/bootstrap:781

```
  778 | };  779 |   780 | // Execute the module function> 781 | modules[moduleId].call(module.exports, module, module.exports, hotCreateRequire(moduleId));      | ^  782 |   783 | // Flag the module as loaded  784 | module.l = true;
```

View compiled

fn

/Users/timloenders/Dropbox/co-own.it/app2/webpack/bootstrap:149

```
  146 | 		);  147 | 		hotCurrentParents = [];  148 | 	}> 149 | 	return __webpack_require__(request);      | ^  150 | };  151 | var ObjectFactory = function ObjectFactory(name) {  152 | 	return {
```

View compiled

Module../src/index.js

http://localhost:3000/static/js/main.chunk.js:129:62

__webpack_require__

/Users/timloenders/Dropbox/co-own.it/app2/webpack/bootstrap:781

```
  778 | };  779 |   780 | // Execute the module function> 781 | modules[moduleId].call(module.exports, module, module.exports, hotCreateRequire(moduleId));      | ^  782 |   783 | // Flag the module as loaded  784 | module.l = true;
```

So probably I skip a step or something. Anyhow in Atom the “const contentclient” doesn’t color the contenclient orange like it should do with a const I would say.

Sorry to bother with this beginners stuff, but I can’t get through this one.

Thanks for any help!

Tim

I already tried to change the connectcontentful script with a require and module.exports, but that didn’t help. I also checked if Contentful was installed and that seems to be fine with 3 contentful top level folders in my application directory.

const contentful = require('contentful');

const SPACE_ID = '123456' 
const ACCESS_TOKEN = 'ABCGDEFG'  
const contentclient = contentful.createClient({
space: SPACE_ID,     
accessToken: ACCESS_TOKEN }) 

module.exports = contentclient;

Does anyone else have an idea maybe?

It looks like your export is the actual contentful client therefore this code cc.contentclient.getEntry('3dImb5wbU7dD1qdllUgBY6') is the equivalent to calling contentclient.contentclient.getEntry(). Try cc.getEntry('3dImb5wbU7dD1qdllUgBY6') and see if it works.