相关疑难解决方法(0)

如何在节点中使用es6导入?

我试图在节点中获取es6导入的挂起,并尝试使用此示例中提供的语法:

Cheatsheet链接:https://hackernoon.com/import-export-default-require-commandjs-javascript-nodejs-es6-vs-cheatsheet-different-tutorial-example-5a321738b50f

我正在查看支持表:http://node.green/,但无法找到支持新导入语句的版本(我尝试查找文本import/require)我目前正在运行节点8.1. 2并且还认为,由于cheatsheet是指.js文件,它应该与.js文件一起使用.

当我运行代码时(取自cheatsheet的第一个例子):

import { square, diag } from 'lib';
Run Code Online (Sandbox Code Playgroud)

我收到错误:SyntaxError:意外的令牌导入.

参考lib我正在尝试导入:

//------ lib.js ------
export const sqrt = Math.sqrt;
export function square(x) {
    return x * x;
}
export function diag(x, y) {
    return sqrt(square(x) + square(y));
}
Run Code Online (Sandbox Code Playgroud)

我缺少什么,如何让节点识别我的import语句?

node.js es6-modules

240
推荐指数
11
解决办法
22万
查看次数

标签 统计

es6-modules ×1

node.js ×1