DockYard / ember-i18n-to-intl-migrator

Migrate ember-i18n to ember-intl
MIT License
21 stars 9 forks source link

Cannot use import statement outside a module #21

Open AddisonG opened 5 years ago

AddisonG commented 5 years ago

After installing the module with sudo npm install ember-i18n-intl-migrator -g, I get the following error when running it (no args):

SyntaxError: Cannot use import statement outside a module
    at new Function (<anonymous>)
    at Object.<anonymous> (/usr/local/lib/node_modules/ember-i18n-intl-migrator/lib/translation-transform.js:12:15)
    at Array.forEach (<anonymous>)
    at module.exports (/usr/local/lib/node_modules/ember-i18n-intl-migrator/lib/translation-transform.js:10:11)
    at Object.<anonymous> (/usr/local/lib/node_modules/ember-i18n-intl-migrator/bin/ember-i18n-intl-migrator.js:10:3)
    at Module._compile (internal/modules/cjs/loader.js:971:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1011:10)
    at Module.load (internal/modules/cjs/loader.js:822:32)
    at Function.Module._load (internal/modules/cjs/loader.js:730:14)
    at Function.Module.runMain (internal/modules/cjs/loader.js:1051:12)

I tried changing directories, from the base ember folder, but just got:

Error: ENOENT: no such file or directory, scandir 'app/locales'

Which makes sense. Thus, I must be in the correct directory, but something else is going wrong. This also happens when I install the module as a local user, along ember-i18n (not globally), and run it from the node_modules folder.

Node Version: v13.0.1 Operating System: Ubuntu 18.04.3 LTS

snewcomer commented 5 years ago

That first error seems like it might be app related code. Not sure where we are using import in translation-transform.js

rreckonerr commented 4 years ago

@AddisonG did you figure out how to deal with this error eventually?

AddisonG commented 4 years ago

@rreckonerr - No, sorry. If I recall correctly, a few of us at work dug down into the code, and found what appeared to be the root cause, but decided against attempting to fix it, since it wasn't worth the effort.

AddisonG commented 4 years ago

Took another look at this @rreckonerr and @snewcomer. The problem seems to be that we were importing other generated json files into translations.js, like so:

import external_import from './example';

export default {
    "first": {
        "key": "value"
    },
    "second": external_import
}

This caused it to fail on this line:

let obj = new Function(content.replace('export default', 'return'))();

The workaround is to manually resolve all the imports, by pasting the contents into the translations.js file, so that it has no import statements or code other than the export default line.

It would be nice to have this work for when multiple files are used, but that's a feature more than a bugfix.