aseemk / requireDir

Node.js helper to require() directories.
MIT License
484 stars 60 forks source link

ES6 version #58

Open Bidek56 opened 5 years ago

Bidek56 commented 5 years ago

Are you planning to upgrade this library to ES6? Is there a similar version of this library written in ES6? Thanks

yocontra commented 5 years ago

@Bidek56 No - does it really matter if this library is using ES5 or ES6? We have more compatibility with older versions of node + no build process right now. Why would we change that?

Bidek56 commented 5 years ago

I think it does, b/c currently I can NOT do import without babel.

import reqdir from 'require-dir';

stephenlacy commented 5 years ago

You can not do imports in node without babel. That is completely separate from this project.

Bidek56 commented 5 years ago

Then, what is "node --experimental-modules" for?

stephenlacy commented 5 years ago

Not es6! https://nodejs.org/api/esm.html

Bidek56 commented 5 years ago

It says: "Node.js contains support for ES Modules based upon the Node.js EP for ES Modules."

yocontra commented 5 years ago

@Bidek56 import reqdir from 'require-dir'; will work just fine, use babel-register in your node project for ES6 imports. A library does not need to "be ES6" to work with imports.

node --experimental-modules is experimental, doesn't work right, etc. we (and the rest of the community) aren't going to jump through hoops to support something nobody uses - just use babel.

brandonros commented 4 years ago
Brandons-MacBook-Pro-2:api brandonros$ node --experimental-modules index.mjs 
(node:10262) ExperimentalWarning: The ESM module loader is experimental.
/Users/brandonros/Desktop/foo/api/node_modules/require-dir/index.js:11
var parentFile = parent.filename;
                        ^

TypeError: Cannot read property 'filename' of undefined
    at Object.<anonymous> (/Users/brandonros/Desktop/foo/api/node_modules/require-dir/index.js:11:25)
    at Module._compile (internal/modules/cjs/loader.js:956:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)
    at Module.load (internal/modules/cjs/loader.js:812:32)
    at Function.Module._load (internal/modules/cjs/loader.js:724:14)
    at ModuleWrap.<anonymous> (internal/modules/esm/translators.js:116:15)
    at ModuleJob.run (internal/modules/esm/module_job.js:109:37)
    at async Loader.import (internal/modules/esm/loader.js:132:24)
import requireDir from 'require-dir'
const routes = requireDir('../routes')
$ node -v
v12.13.0
node --experimental-modules index.mjs 

image

module.parent is undefined when trying to use with ECMAScript modules

yocontra commented 4 years ago

@brandonros See above comment - it is labeled experimental, I will update the package when it is finalized and marked as stable.

Kostanos commented 2 years ago

Any updates on this? did someone find the replacement for this module, that works with ES6?

thank you

yocontra commented 2 years ago

I'll reopen this, since it's been 3 years and this is no longer an experimental node feature. Likely to support modules requireDir will need to become an async function, so top-level await was needed.