相关疑难解决方法(0)

Node.js - SyntaxError:意外的令牌导入

我不明白出了什么问题.节点v5.6.0 NPM v3.10.6

代码:

function (exports, require, module, __filename, __dirname) {
    import express from 'express'
};
Run Code Online (Sandbox Code Playgroud)

错误:

SyntaxError: Unexpected token import
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:387:25)
    at Object.Module._extensions..js (module.js:422:10)
    at Module.load (module.js:357:32)
    at Function.Module._load (module.js:314:12)
    at Function.Module.runMain (module.js:447:10)
    at startup (node.js:140:18)
    at node.js:1001:3
Run Code Online (Sandbox Code Playgroud)

javascript node.js npm ecmascript-6

388
推荐指数
11
解决办法
52万
查看次数

如何在节点中使用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万
查看次数

标签 统计

node.js ×2

ecmascript-6 ×1

es6-modules ×1

javascript ×1

npm ×1