atdatabases icon indicating copy to clipboard operation
atdatabases copied to clipboard

pg-migrations with TypeScript migrations from esm only package fails to import

Open bhvngt opened this issue 3 years ago • 1 comments

I am trying to import a module from an esm package. My project is in itself an esm package. I am getting following error

Error [ERR_REQUIRE_ESM]: require() of ES Module <package_file> from src/migrations/0001-init.ts not supported.

package.json of the imported package is as follows

	"type": "module",
	"main": "./dist/index.js",
	"exports": {
		".":  "./dist/index.js",
		"./*": "./dist/*.js"
	},
	"typesVersions": {
		"*": {
			"*": ["dist/*"]
		}
	},

migration project itself is a esm package containing type: module in its package.json

I am running the pg-migration using following cli pg-migrations apply -D src/migrations -c <connection_string>

If I remove type: module from the package.json of imported package, then it all works.

Would anyone have any workaround for this.

bhvngt avatar Oct 18 '21 15:10 bhvngt

I think the only option where would be to use .mjs for your modules, instead of relying on "type": "module" in package.json. I don't think there's any obvious way to make pg-migrations work with both CommonJS and ESModules at the same time.

ForbesLindesay avatar Feb 15 '22 11:02 ForbesLindesay