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

import from, export class #1000

Closed
Shin1gami opened this issue Feb 28, 2015 · 10 comments
Closed

import from, export class #1000

Shin1gami opened this issue Feb 28, 2015 · 10 comments
Labels
question Issues that look for answers.

Comments

@Shin1gami
Copy link

Let's say we want to usea module with class like:

export class Test {
    constructor() {
    }
}

And load it:

import {test} from 'test.js';
new Test();

But when we try to run it:

iojs --use_strict --harmony --es_staging server.js

We've got an error:

export class Test {
^^^^^^
SyntaxError: Unexpected reserved word
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:427:25)
    at Object.Module._extensions..js (module.js:462:10)
    at Module.load (module.js:339:32)
    at Function.Module._load (module.js:294:12)
    at Module.require (module.js:349:17)
    at require (module.js:368:17)
    at Object.<anonymous> (server.js:4:1)
    at Module._compile (module.js:444:26)
    at Object.Module._extensions..js (module.js:462:10)

So, can we use such syntax?

@bnoordhuis
Copy link
Member

Not at the moment, the ES6 module system in V8 is still in its infancy. Your example can probably be made to parse by passing --harmony_modules on the command line but I won't vouch for it actually working.

@JiangJie
Copy link

JiangJie commented Jun 1, 2015

@bnoordhuis It appears that --harmony_modules is still not working at iojs v2.2.0.

import {param} from '../config/param';
^^^^^^
SyntaxError: Unexpected token import

@navaru
Copy link

navaru commented Jun 28, 2015

Is there a plan to implement the ES6 module syntax when V8 provides support?

A lot of module authors have begun to write modules with ES6 syntax, adding a special build process to enable a package to be used in io.js / node adds complexity and segmentation in usability, it feels like the whole CoffeeScript-ish era again 😄.

@ChALkeR ChALkeR added the question Issues that look for answers. label Jun 28, 2015
@bnoordhuis
Copy link
Member

@teugen There's been some discussion about that here: nodejs/NG#5

@MadLittleMods
Copy link
Contributor

👍

Running into the reserved word SyntaxError in iojs v2.3.3 even with the --harmony_modules flag.


An easy solution for now is to use babel-tape-runner or for single files, 6to5, and either can be used nicely in a script in package.json:

npm run execute-script

{
  "scripts": {
    "execute-script": "babel-tape-runner index.js || exit 0"
  }
}
// ...
"scripts": {
    "execute-script": "6to5 src/index.js | iojs"
},

@trevnorris
Copy link
Contributor

I'm fairly certain that iojs will need to hook into V8 to specify how to resolve the module path. This has not been done, and don't think the hooks exist yet.

@cirano-eusebi
Copy link

@MadLittleMods I tryed what you said but kept having the same error...

Is there something wrong in the declarations that I'm doing?

/*CliParser.js*/

export class CliParser {

   static parse(arguments) {
   //Code goes here
   }
}
/*index.js*/

import {CliParser} from './CliParser.js';

//Parse the argument vector
var parsedArguments = CliParser.parse(process.argv);

I've done as you said and added a script in my package.json file:

/*package.json*/

"scripts": {
    "IPtoNode" : "6to5 index.js | iojs",
    "test": "echo \"Error: no test specified\" && exit 1"
},

This is what i get when i run the script.

#console

npm run IPtoNode 
PathToApp\IPtoNode\CliParser.js:1
(function (exports, require, module, __filename, __dirname) { export class Cli
                                                              ^^^^^^
SyntaxError: Unexpected reserved word
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:413:25)
    at Object.Module._extensions..js (module.js:448:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Module.require (module.js:365:17)
    at require (module.js:384:17)
    at [stdin]:12:17
    at Object.exports.runInThisContext (vm.js:54:17)
    at Object.<anonymous> ([stdin]-wrapper:6:22)

Note that if I run it out of the npm script it also produces the same error.

@trevnorris
Copy link
Contributor

The V8 API does not exist to allow us to define what happens with the path passed to import, so V8 has no idea how to resolve where the module is you're requesting.

@MadLittleMods
Copy link
Contributor

@MagoDopado It is the because the dependency, CliParser.js, is not getting transpiled by babel. You can use babel-tape-runner

npm run execute-script

{
  "scripts": {
    "execute-script": "babel-tape-runner index.js || exit 0"
  }
}

@Teomazivila
Copy link

Hi Everyone, I have an issue that is related to this topic. I am creating a an ASPNETCORE Angular app, and getting the bellow error when executing DOTNET WATCH RUN
[error] D:\TeoProjects.Net\AspNet\Teomaz_C\ClientApp\node_modules\bluebird\js\release\reduce.js:1
(function (exports, require, module, __filename, __dirname) {

SyntaxError: Invalid or unexpected token
at new Script (vm.js:80:7)
at createScript (vm.js:274:10)
at Object.runInThisContext (vm.js:326:10)
at Module._compile (internal/modules/cjs/loader.js:664:28)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
at Function.Module._load (internal/modules/cjs/loader.js:531:3)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:22:18)
at module.exports (D:\TeoProjects.Net\AspNet\Teomaz_C\ClientApp\node_modules\bluebird\js\release\promise.js:788:1)
at Object. (D:\TeoProjects.Net\AspNet\Teomaz_C\ClientApp\node_modules\bluebird\js\release\bluebird.js:9:36)
at Module._compile (internal/modules/cjs/loader.js:701:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)

I have tried:

  1. npm install
  2. Deleting the node_modules and and reinstalling them again
    I tried a couple more options that I can't remember that were suggested on Stackoverflow.
    I am quite new on this topic, so please take it easy on me. Thanks in advanced

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

No branches or pull requests

10 participants