Add Support for .CJS?
szarapka opened this issue ยท 3 comments
When running Node 14 (or 13) and utilizing ECMAScript Modules with a field "type": "module"
in the top-level package.json
, this error is thrown:
/node_modules/mongo-seeding/dist/index.js:111
const error = new Error(`${err.name}: ${err.message}`);
^
Error [MongoSeedingError]: Error: Must use import to load ES Module: /data/users/default.js
require() of ES modules is not supported.
require() of /data/users/default.js from /node_modules/mongo-seeding/dist/populator/filesystem.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename default.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from package.json.
at wrapError (/node_modules/mongo-seeding/dist/index.js:111:19)
at Seeder.readCollectionsFromPath (/node_modules/mongo-seeding/dist/index.js:53:23)
at file:///seed.js:20:28
at ModuleJob.run (internal/modules/esm/module_job.js:110:37)
at async Loader.import (internal/modules/esm/loader.js:164:24) {
name: 'MongoSeedingError'
}
It isn't possible for me to remove "type": "module"
as that breaks the ES6 import/export functionality. Ideally, support for import/export in the core of the library would be awesome but would be a time-consuming rewrite of the importer and other elements. In the interim, is it possible that .CJS
file support could be added?
I think it should be a single line change in the /core/src/config.ts
file, adding .cjs
to the extensions array, but I haven't tried it myself, yet.
Happy to submit a PR if this support is deemed acceptable to add.
Thanks!
Hi @szarapka,
You don't have to modify code to configure which file extensions you want to support during data import. Please add cjs
to the extensions
in config while reading collections from a given path: https://github.com/pkosiec/mongo-seeding/tree/master/core#readcollectionsfrompathpath-partialoptions
Let me know if this works for you. Cheers!
Hi @szarapka,
You don't have to modify code to configure which file extensions you want to support during data import. Please add
cjs
to theextensions
in config while reading collections from a given path: https://github.com/pkosiec/mongo-seeding/tree/master/core#readcollectionsfrompathpath-partialoptionsLet me know if this works for you. Cheers!
@pkosiec That worked, I don't know how I missed that when reading through the docs, thanks! I would still suggest considering making it a default option for the package until you support ES6 modules.
Thanks for all the great work, mongo-seeding is awesome!
Thanks for the very kind words. I'm glad you like this project ๐
I would still suggest considering making it a default option for the package until you support ES6 modules.
I agree, as this will work also on lower versions of Node.js. Here's the PR: #120. cjs
support will be enabled by default in future Mongo Seeding 3.5.0 ๐