【问题标题】:Problems with the import syntax Node js导入语法Node js的问题
【发布时间】:2019-03-15 12:43:52
【问题描述】:

我正在尝试使用 node.js 探测一个 npm 数据包。数据包是this,但是当我在执行 node script.js 时,控制台上会出现以下错误。这就是我想要做的:

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' }
})

错误如下:

(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

我已经在 node_modules 目录中安装了这两个包,我认为错误是因为解释器不理解最近的 ecmascript,我必须按照要求来做,但我已经安装了最新版本的节点。知道会发生什么吗?谢谢

node --version                                                           10:43
v11.10.1

更新:我也尝试使用实验标志来执行此操作,但会产生此错误。

错误

(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)

【问题讨论】:

标签: node.js


【解决方案1】:

您缺少在启用--experimental-modules 标志的情况下运行节点应用程序。否则 import 对于节点是未知的,您可以在此处看到类似的答案 https://stackoverflow.com/a/54384132/4229159

Read here about import compatibility

【讨论】:

  • 哦,我使用的是实验性的,但我仍然有问题(节点:9257) ExperimentalWarning:ESM 模块加载器是实验性的。 xx:1 (function (exports, require, module, __filename, __dirname) { import { get } from 'http'; ^ SyntaxError: Unexpected token {
  • 哦!那么我在阅读问题时错过了这一点,也许您可​​以更新问题,添加您在那里有实验标志但仍然失败?
猜你喜欢
  • 2011-08-29
  • 2020-10-19
  • 1970-01-01
  • 2017-12-01
  • 1970-01-01
  • 2020-11-12
  • 1970-01-01
  • 1970-01-01
  • 2021-09-25
相关资源
最近更新 更多