Skip to content

node --experimental-modules don't give the code where to find the error, just Invalid or unexpected token. Eg v.8.9.1 #17277

Description

@webmobiles

I'm testing projects renaming js to .mjs in order to execute them with --experimental-modules
they are proyects running ok with babel.

But I keep guessing where the error can be found on my code. I get always an error that it's hard to find:

(node:16709) ExperimentalWarning: The ESM module loader is experimental.
SyntaxError: Invalid or unexpected token
at ModuleJob.loaders.set [as moduleProvider] (internal/loader/ModuleRequest.js:32:13)
at

by example I can copy this example code where the error is produced, but it's not only with this situation but in anothers too. Why Node don't give me the line and the code that produces the error ? maybe it's better I try with node 9 ? that version is more compatible with --experimental-modules ?

Example code:

test.js

import { pgClient } from '../connection.mjs';
//or 
//import { pgClient } from '../connection';

const pool = pgClient();

(async function(){
  const client = await pool.connect();
  const res = await client.query('
  SELECT table_name
  FROM information_schema.tables
  WHERE table_schema = 'public');
  console.log( JSON.stringify(res.rows[0]));

  client.release();
})();

await pool.end();

connection.jms (this file have errors , by example using require() or anothers, but the idea is to get display errors that help you to find errors )

import dotenv from 'dotenv';
dotenv.config();
import Sequelize from 'sequelize';
//import { Pool, Client } from 'pg';

export const db = new Sequelize(process.env.DB_NAME, process.env.DB_USER, process.env.DB_PASS, {
  host: 'localhost',
  port: '5432',
  dialect: 'postgres',
  timezone: '+00:00',
  pool: {
    max: 5,
    min: 0,
    idle: 10000
  },
});


// conection segurisé direct verst postresql without graphl, example dont' expose password for users
export const pgClient = () => {
  
  const { Pool, Client } =  require('pg');
  const pool = new Pool()
  
  return pool;
};

I execute with:

node --experimental-modules ./src/test.mjs

again I repeat the error message:


(node:17571) ExperimentalWarning: The ESM module loader is experimental.
SyntaxError: Invalid or unexpected token
    at ModuleJob.loaders.set [as moduleProvider] (internal/loader/ModuleRequest.js:32:13)
    at <anonymous>

Version: 8.9.1
Platform: Mac ElCapitan

Activity

  1. devsnek commented on Nov 23, 2017

    @devsnek
    Member

    the await pool.end(); is your invalid token, await can only occur in async function bodies. I will agree however, that the error provided is not very good.

  2. webmobiles commented on Nov 23, 2017

    @webmobiles
    Author

    yes, but my post is about node does not show where is the error, file and line...

  3. added
    esmIssues and PRs related to the ECMAScript Modules implementation.
    on Nov 23, 2017
  4. bnoordhuis commented on Nov 23, 2017

    @bnoordhuis
    Member

    Okay, so the test case can be simplified to:

    await async () => {};  // t.mjs

    And then you get a decent error message with:

    $ node t.mjs 
    /home/bnoordhuis/src/t.mjs:1
    (function (exports, require, module, __filename, __dirname) { await async () => {};  // t.mjs
                                                                  ^^^^^
    
    SyntaxError: await is only valid in async function
        at createScript (vm.js:80:10)
        # ...
    

    And a rather less useful one with:

    $ node --experimental-modules t.mjs 
    (node:86549) ExperimentalWarning: The ESM module loader is experimental.
    SyntaxError: Unexpected reserved word
        at ModuleJob.loaders.set [as moduleProvider] (internal/loader/ModuleRequest.js:32:13)
        at <anonymous>
    

    The error message is slightly different when it's in an imported module but it's the same basic bug.

  5. webmobiles commented on Nov 24, 2017

    @webmobiles
    Author

    okey, I get the error in another situations too with no detailed error message, my post is not to get support about the await problem... but I this a bug ? why with --experimental-modules i dont get the error ? what about if i want to start a new project just with --experimental-modules ?

  6. bnoordhuis commented on Nov 24, 2017

    @bnoordhuis
    Member

    @webmobiles See #17281, it's going to get fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmed-bugIssues and PRs for confirmed bugs.esmIssues and PRs related to the ECMAScript Modules implementation.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions