awilix icon indicating copy to clipboard operation
awilix copied to clipboard

Unexpected token 'export'

Open ksuewer opened this issue 2 years ago • 8 comments

This error happens on startup since version 6.1.0:

/project/dist/modules/foo/repository/FooBarRepository.d.ts:1
export default class FooBarRepository {
^^^^^^

SyntaxError: Unexpected token 'export'
    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1032:15)
    at Module._compile (node:internal/modules/cjs/loader:1067:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1157: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._loadModulesDeps.require (/project/node_modules/awilix/lib/container.js:342:28)
    at /project/node_modules/awilix/lib/load-modules.js:60:41

Might it have to do with replacing glob by fast-glob ?

My project uses Typescript and I use loadModules to load classes:

container.loadModules([
  'modules/**/repository/!(*.spec|*.d|*.test).{js,ts}',
  'modules/**/service/!(*.spec|*.d|*.test).{js,ts}',
], {
  cwd: path.join(__dirname, '../'),
  formatName: 'camelCase',
  resolverOptions: {
    lifetime: awilix.Lifetime.SINGLETON,
    injectionMode: 'CLASSIC',
    register: awilix.asClass,
  },
});

ksuewer avatar Apr 26 '22 12:04 ksuewer

Looks like you are trying to load es modules as if they are cjs?

What are you transpiling to? Are you using ts node? I don’t have much to go on.

jeffijoe avatar Apr 26 '22 12:04 jeffijoe

I'm compiling with tsc. With awilix 6.0.0 everything is fine.

ksuewer avatar Apr 26 '22 12:04 ksuewer

Seems like the *.d.ts files are not excluded any more. Maybe the syntax changed.

ksuewer avatar Apr 26 '22 12:04 ksuewer

Its possible that fast-glob isn’t respecting that ignore pattern then. I haven’t had to add those myself though and I also use TypeScript.

jeffijoe avatar Apr 26 '22 12:04 jeffijoe

Yes, seems like there are several problems with fast-glob ignore patterns like this issue which is close to my issue: https://github.com/mrmlnc/fast-glob/issues/357
I fixed it meanwhile by limiting to js files only :)

ksuewer avatar Apr 26 '22 12:04 ksuewer

We will have to wait until the issue is fixed in fast-glob.

jeffijoe avatar May 02 '22 10:05 jeffijoe

FYI the issue of fast-glob has been closed, but the release has not happened (although it was mentioned). Maybe it is best to ping them https://github.com/mrmlnc/fast-glob/issues/357#issuecomment-1152909138

davidzwa avatar Jul 02 '22 11:07 davidzwa

I left a comment but no news yet.

jeffijoe avatar Aug 01 '22 10:08 jeffijoe