You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
node --experimental-modules don't give the code where to find the error, just Invalid or unexpected token. Eg v.8.9.1 #17277
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>
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.
$ 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.
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 ?
Activity
devsnek commentedon Nov 23, 2017
the
await pool.end();is your invalid token,awaitcan only occur in async function bodies. I will agree however, that the error provided is not very good.webmobiles commentedon Nov 23, 2017
yes, but my post is about node does not show where is the error, file and line...
bnoordhuis commentedon Nov 23, 2017
Okay, so the test case can be simplified to:
And then you get a decent error message with:
And a rather less useful one with:
The error message is slightly different when it's in an imported module but it's the same basic bug.
webmobiles commentedon 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 commentedon Nov 24, 2017
@webmobiles See #17281, it's going to get fixed.
module: print better message on esm syntax error
module: print better message on esm syntax error
module: print better message on esm syntax error