clintandrewhall/node-foursquare

'TypeError: require(...) is not a function' error with 0.4.* version

Closed this issue · 7 comments

when using 0.4.* version in a CommonJS setup the error 'TypeError: require(...) is not a function' appears:
sahat/hackathon-starter#888
The project is written using CommonJS setup:

$ npm start

> hackathon-starter@5.1.2 start /mnt/c/temp/hackathon-starter
> node app.js

/mnt/c/temp/hackathon-starter/controllers/api.js:16
const { Venues, Users } = require('node-foursquare')({
                                                    ^

TypeError: require(...) is not a function
    at Object.<anonymous> (/mnt/c/temp/hackathon-starter/controllers/api.js:16:53)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at Object.<anonymous> (/mnt/c/temp/hackathon-starter/app.js:35:23)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
    at startup (internal/bootstrap/node.js:266:19)

Can the Babel setup be configured to support default module exports for ES5/CommonJS:
https://babeljs.io/docs/en/babel-plugin-transform-es2015-modules-commonjs.html
Otherwise it breaks backward compatibility

Thanks!

@clintandrewhall it seems like that a work around is to have consumers of node-foursquare to add babel-polyfill as their dependency (babel/babel#5085) is there a way to add a fix in node-foursquare so the consumers don't have to declare extra dependencies? Alternatively if the backward compatibility is to stay broken, can you give an example of how to do the upgrade or how to use the version of the module?

#38 Is the fix, and I'll have the changes published to npm momentarily.

v0.4.4 published to npm... I confirmed the change works with hackathon-starter... but be aware: babel is pushing for the ES6 standard, and devs are following suit. Hopefully node can catch up sooner rather than later.

@peterblazejewicz incidentally, the fix for this was with babel-plugin-add-module-exports. I appreciate the detailed bug report! Made the fix easier.

@YasharF done and done. :-)

@clintandrewhall
Clint, shouldn't the babel-polyfill be made a dependency instead of being dev in your setup?
https://github.com/clintandrewhall/node-foursquare/blob/master/package.json#L40
Otherwise one needs to include it when using node-foursquare:

Error: Cannot find module 'babel-polyfill'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:581:15)
    at Function.Module._load (internal/modules/cjs/loader.js:507:25)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at Object.<anonymous> (/Users/piotrblazejewicz/git/hackathon-starter/node_modules/node-foursquare/dist/index.js:7:22)

Because this is a polyfill (which will run before your source code), we need it to be a dependency, not a devDependency
https://babeljs.io/docs/en/babel-polyfill/

I think bable polyfill was the main issue that I was hoping to have fixed yesterday. Adding .default to the import from the consumer end seemed reasonable to be inline with ES6 requirments. Hackathon starter has already moved to a lot of ES6 syntax already and that is more of the direction for the project. However we also need to keep it simple and avoid increasing the size via dependencies as much as possible.

Doh. I'll get this fixed and pushed in a moment. The app I use with this library, the whole reason I wrote it in the first place, doesn't need it, so I missed it. And everything was working with hackaton-starter. :-/

Fixed and published as v0.4.5