Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SyntaxError: Unexpected token 'export' #417

Open
joezappie opened this issue Jun 15, 2022 · 3 comments
Open

SyntaxError: Unexpected token 'export' #417

joezappie opened this issue Jun 15, 2022 · 3 comments

Comments

@joezappie
Copy link

Describe the bug
I am getting an error about using import in a project that uses ESM.

SyntaxError: Unexpected token 'export'
    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1033:15)
    at Module._compile (node:internal/modules/cjs/loader:1069:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.require (/workspaces/mrp/node_modules/migrate-mongo/lib/utils/module-loader.js:3:12)
    at Object.loadMigration (/workspaces/mrp/node_modules/migrate-mongo/lib/env/migrationsDir.js:101:27) {
  migrated: []
}

I see line 101 in migrationsDir is doing a require thats surrounded by a try catch. Doesn't seem like e.code === ERR_REQUIRE_ESM as its not running that and rethrowing the error.

try {
      return moduleLoader.require(migrationPath);
    } catch (e) {
      if (e.code === 'ERR_REQUIRE_ESM') {
        return moduleLoader.import(url.pathToFileURL(migrationPath));
      }
      throw e;
    }

Since it seems like the moduleSystem config variable was recently added, how about using that instead of a try catch to run the appropriate loader?

const configContent = await config.read();
switch(configContent.moduleSystem) {
   case "esm":
      return moduleLoader.import(url.pathToFileURL(migrationPath));
      break;
   case "commonjs":
      return moduleLoader.require(migrationPath);
      break;
}

To Reproduce
I am using this via the programmable API.

migrate.config.set(this.config);
const { db, client } = await migrate.database.connect();
await migrate.up(db, client);

Expected behavior
Should properly import when in esm environment.

Additional context
I am running this as a bin script. Package.json is set to type module and my config file for migrate-mongo has moduleSystem: 'esm'.

@deiay
Copy link

deiay commented Jul 22, 2022

I'm having the same issue

@lukjaki
Copy link

lukjaki commented Oct 2, 2023

I'm experiencing similar issue.

@onurcskun
Copy link

I'm having the same issue as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants