mean-expert-official / loopback-sdk-builder

Tool for auto-generating Software Development Kits (SDKs) for LoopBack
Other
399 stars 175 forks source link

Unexpected token import #543

Open pharindoko opened 6 years ago

pharindoko commented 6 years ago

What type of issue are you creating?

What version of this module are you using?

Please add a description for your issue:

I received following exception when I was executing the sdk builder with default settings:

THE MAGIC IS ABOUT TO HAPPEN ...

/home/flo/catalogs/MobilityServiceCatalog/api-main/src/common/models/mobserviceuser.js:2
import { authenticateUser, findUser } from "../../server/boot/ldap.conf";
^^^^^^

SyntaxError: Unexpected token import
    at createScript (vm.js:74:10)
    at Object.runInThisContext (vm.js:116:10)
    at Module._compile (module.js:533:28)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:503:32)
    at tryModuleLoad (module.js:466:12)
    at Function.Module._load (module.js:458:3)
    at Module.require (module.js:513:17)
    at require (internal/module.js:11:18)
    at requireNodeOrEsModule (/home/flo/catalogs/MobilityServiceCatalog/api-main/node_modules/loopback-boot/lib/require.js:7:17)
    at /home/flo/catalogs/MobilityServiceCatalog/api-main/node_modules/loopback-boot/lib/executor.js:248:20
    at Array.forEach (native)
    at defineModels (/home/flo/catalogs/MobilityServiceCatalog/api-main/node_modules/loopback-boot/lib/executor.js:229:23)
    at setupModels (/home/flo/catalogs/MobilityServiceCatalog/api-main/node_modules/loopback-boot/lib/executor.js:197:3)
    at execute (/home/flo/catalogs/MobilityServiceCatalog/api-main/node_modules/loopback-boot/lib/executor.js:40:3)
    at bootLoopBackApp (/home/flo/catalogs/MobilityServiceCatalog/api-main/node_modules/loopback-boot/index.js:154:3)

The reason is that I`m using import statemtent (es6 with Babel) in one of the Model.js files ....

'use strict';
import { authenticateUser, findUser } from "../../server/boot/ldap.conf";

Anyone has a clue how to fix it and keep this es6 feature ?

Greetings,

Flo

fleita9 commented 6 years ago

Same problem with babel... have you found a workaround?

jonathan-casarrubias commented 6 years ago

Hey guys, thanks for reaching out... Well that is an extremely weird issue,

I don't even see any file provided by the SDK builder being executed in that stack, I'm not entirely sure what is related since is not directly related to the SDK.

But, indirectly the SDK does run your app, to get all the model instances and rules you define, and then iterate those in order to build an SDK... At this point I don't even see the boot gets to finish.

So my assumption is: You need to either transpile your models, and then point that transpiled source within the SDK, instead of the ES6 one you are using OR execute the SDK with babel as well instead of pure node command.

I hope this information guides you to find the fix.

Cheers Jon

fleita9 commented 6 years ago

Hi @jonathan-casarrubias, thanks for your comments. In order to use babel, I've made an app.js file that imports babel and requires server/server.js. In fact, the error that i'm gettin is this one when I run lb-sdk with app.js file as first argument:

/mnt/sda6/fleita/Desarrollo/Humber/api/node_modules/@mean-expert/loopback-sdk-builder/bin/lb-sdk:98
  apiUrl: argv['url'] || app.get('restApiRoot') || '/api',
                             ^

TypeError: app.get is not a function

I think I should fork this repo, version 2.2.1, so that i can get the loopback app reference correctly.

fleita9 commented 6 years ago

var app = require(appFile).default; made the trick.

Gracias!

jonathan-casarrubias commented 6 years ago

@fleita9 so cool you were able to fix it. I'll keep this issue open for some time, so anyone having this issue can have the reference.

Cheers Jon