0

I'm trying to probe a npm packet with node. The packet is this but when I'm doing the node script.js the following error appear on the console. This is what I'm trying to do:

import { get } from 'http'
import { http } from 'get-headers'

get('http://example.com', (res) => {
  http(res) //=> { 'Accept-Ranges': 'bytes', 'Cache-Control': 'max-age=604800', 'Content-Type': 'text/html', Date: 'Mon, 17 Aug 2015 19:53:03 GMT', Etag: '"359670651"', Expires: 'Mon, 24 Aug 2015 19:53:03 GMT', 'Last-Modified': 'Fri, 09 Aug 2013 23:54:35 GMT', Server: 'ECS (rhv/818F)', 'X-Cache': 'HIT', 'x-ec-custom-error': '1', 'Content-Length': '1270', Connection: 'close' }
})

The error is the following:

(function (exports, require, module, __filename, __dirname) { import { get } from 'http';
                                                                     ^

SyntaxError: Unexpected token {
    at new Script (vm.js:85:7)
    at createScript (vm.js:266:10)
    at Object.runInThisContext (vm.js:314:10)
    at Module._compile (internal/modules/cjs/loader.js:698:28)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:749:10)
    at Module.load (internal/modules/cjs/loader.js:630:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:570:12)
    at Function.Module._load (internal/modules/cjs/loader.js:562:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:801:12)
    at internal/main/run_main_module.js:21:11

I've installed the two packages in the node_modules dir, I think that the mistake is because the interpreter doesn't understand the recent ecmascript and I've to do it whit the require but I've installed the latest version of node. Any idea of what could be happening?. Thank you

node --version                                                           10:43
v11.10.1

UPDATE: I also tried to do it with the experimental flag but it yields this error.

ERROR

(node:12479) ExperimentalWarning: The ESM module loader is experimental.
/home/...:1
(function (exports, require, module, __filename, __dirname) { import { get } from 'http';
                                                                     ^

SyntaxError: Unexpected token {
    at new Script (vm.js:85:7)
    at createScript (vm.js:266:10)
    at Proxy.runInThisContext (vm.js:314:10)
    at Module._compile (internal/modules/cjs/loader.js:698:28)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:749:10)
    at Module.load (internal/modules/cjs/loader.js:630:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:570:12)
    at Function.Module._load (internal/modules/cjs/loader.js:562:3)
    at createDynamicModule (internal/modules/esm/translators.js:78:15)
    at Object.meta.done (internal/modules/esm/create_dynamic_module.js:40:9)
Gerard Rozsavolgyi
  • 4,154
  • 4
  • 29
  • 35
elvaqueroloconivel1
  • 769
  • 1
  • 7
  • 11
  • 2
    Possible duplicate of [How can I use an es6 import in node?](https://stackoverflow.com/questions/45854169/how-can-i-use-an-es6-import-in-node) – raina77ow Mar 15 '19 at 12:45

1 Answers1

0

You are missing running the node app with the --experimental-modules flag enabled. Otherwise import is unknown for node you can see here a similar answer https://stackoverflow.com/a/54384132/4229159

Read here about import compatibility

Alejandro Vales
  • 2,546
  • 17
  • 31
  • oh, I use the experimental but I still have the problem (node:9257) ExperimentalWarning: The ESM module loader is experimental. xx:1 (function (exports, require, module, __filename, __dirname) { import { get } from 'http'; ^ SyntaxError: Unexpected token { – elvaqueroloconivel1 Mar 15 '19 at 12:53
  • Oh! then I missed that while reading the question, maybe you could update the question adding that you have the experimental flag there and still fails? – Alejandro Vales Mar 15 '19 at 12:54