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

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

Closed
webmobiles opened this issue Nov 23, 2017 · 5 comments
Labels
confirmed-bug Issues with confirmed bugs. esm Issues and PRs related to the ECMAScript Modules implementation.

Comments

@webmobiles
Copy link

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

@devsnek
Copy link
Member

devsnek commented Nov 23, 2017

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.

@webmobiles
Copy link
Author

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

@mscdex mscdex added the esm Issues and PRs related to the ECMAScript Modules implementation. label Nov 23, 2017
@bnoordhuis
Copy link
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.

@webmobiles
Copy link
Author

webmobiles commented Nov 24, 2017

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 ?

@bnoordhuis
Copy link
Member

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

bnoordhuis added a commit to bnoordhuis/io.js that referenced this issue Nov 29, 2017
Include the offending line in the output and underline the bad token.

Before this commit, it printed "SyntaxError: Unexpected reserved word"
without indicating where the syntax error is.

Now it prints the line and underlines the offending token, like it does
for syntax errors in CJS scripts.

Minor changes are made to the test runner in order to support `*.mjs`
files in test/message.

Fixes: nodejs#17277
PR-URL: nodejs#17281
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Evan Lucas <evanlucas@me.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
@bnoordhuis bnoordhuis added the confirmed-bug Issues with confirmed bugs. label Nov 29, 2017
MylesBorins pushed a commit that referenced this issue Dec 12, 2017
Include the offending line in the output and underline the bad token.

Before this commit, it printed "SyntaxError: Unexpected reserved word"
without indicating where the syntax error is.

Now it prints the line and underlines the offending token, like it does
for syntax errors in CJS scripts.

Minor changes are made to the test runner in order to support `*.mjs`
files in test/message.

Fixes: #17277
PR-URL: #17281
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Evan Lucas <evanlucas@me.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
MylesBorins pushed a commit that referenced this issue Dec 12, 2017
Include the offending line in the output and underline the bad token.

Before this commit, it printed "SyntaxError: Unexpected reserved word"
without indicating where the syntax error is.

Now it prints the line and underlines the offending token, like it does
for syntax errors in CJS scripts.

Minor changes are made to the test runner in order to support `*.mjs`
files in test/message.

Fixes: #17277
PR-URL: #17281
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Evan Lucas <evanlucas@me.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug Issues with confirmed bugs. esm Issues and PRs related to the ECMAScript Modules implementation.
Projects
None yet
Development

No branches or pull requests

4 participants