Javascript node.js 中的意外保留字导入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32346886/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 07:55:39  来源:igfitidea点击:

unexpected reserved word import in node.js

javascriptnode.jsecmascript-6importerror

提问by Dracontis

I'm trying to run node.js backend server. I've received error unexpected reserved wordon import in Node.js file.

我正在尝试运行 node.js 后端服务器。我unexpected reserved word在 Node.js 文件中导入时收到错误。

The lines in file core.module.jsis:

文件中的行core.module.js是:

'use strict';
import lodashMixins from './lodashMixins.js'
... other imports and configurations ...

I launch simple command: node core.module.js

我启动简单的命令: node core.module.js

It's not uncommon error, but usually it happens with other libraries. I haven't seen solution for Node.js. How should I fix this? I'm using Windows Server.

这种错误并不少见,但通常发生在其他库中。我还没有看到 Node.js 的解决方案。我应该如何解决这个问题?我正在使用 Windows 服务器。

Edit:I've find out that it's ES6, but how could I launch it? It looks like backend for the application, but I have no idea what command should I use to launch it without errors.

编辑:我发现它是 ES6,但我怎么能启动它呢?它看起来像应用程序的后端,但我不知道应该使用什么命令来启动它而不会出错。

采纳答案by Amit

The importkeyword is part of the modules feature in ECMAScript 2015, along with exportand a few other specifications.

import关键字是ECMAScript 2015 中模块功能的一部分,以及export其他一些规范。

It is currently not implemented natively in NodeJS, even on the lastest version (v0.12.7), nor is it supported in the ES2015 "friendlier" fork iojs.

它目前未在 NodeJS 中本地实现,即使在最新版本 (v0.12.7) 中,也未在 ES2015“更友好”的 fork iojs 中支持。

You will need to use a transpiler to get that to work.

您将需要使用转译器来使其工作。

[edit] it's still unsupported in the latest version (v5.8) despite the existence of an --harmony_modulesflag, which does nothing. Your best run is to use babel, as explained hereand here

[编辑] 尽管存在一个--harmony_modules标志,但它在最新版本 (v5.8) 中仍然不受支持,它什么也不做。您最好的方法是使用 babel,如此此处所述

回答by Sami

importis a part of ECMAScript 2015 (ES6) standardand as Amitabove mentioned it is not currently implemented natively in Nodejs.

import是的一部分ECMAScript 2015 (ES6) standardAmit上述当前未在本地实现的NodeJS。

So you can use transpiler like babelto run your es6script

所以你可以使用 transpiler likebabel来运行你的es6脚本

npm install babel

npm install babel

An example based on this answer

基于此答案的示例

app.js

应用程序.js

 import {helloworld,printName} from './es6'
 helloworld();
 printName("John");

es6.js

es6.js

 module.exports = {
    helloworld: function() { console.log('hello world!'); },
    printName: function(name) { console.log(name); }
}

And using require hookin start.js

require hookstart.js 中使用

require("babel/register");
var app = require("./app.js");

And start your app as

并启动您的应用程序

node start.js

EDITThe above answer was base on babel v5.8.23. For babel >= v6

编辑上面的答案是基于babel v5.8.23. 为了babel >= v6

Use require hookin start.jsas

使用require hookstart.js作为

require('babel-core/register');
require("./app.js");

Also, transformations are not enabled by default. So you will need to install a preset. In this case use es2015

此外,默认情况下不启用转换。所以你需要安装一个preset. 在这种情况下使用es2015

npm install babel-preset-es2015

And use it in a .babelrcfile in root folder

.babelrc在根文件夹中的文件中使用它

{
   "presets": ["es2015"]
}

回答by sree

I ran into this issue as I manually install any of these tools outside of Visual Studio. But Visual Studio ships with multiple open source command line tools that are used in modern web development workflows. Here's how you can tell Visual Studio to use the same version that you have manually installed

我在 Visual Studio 之外手动安装任何这些工具时遇到了这个问题。但是 Visual Studio 附带了多个用于现代 Web 开发工作流的开源命令行工具。以下是告诉 Visual Studio 使用手动安装的相同版本的方法

Go to Tools –> Options –> Projects and Solutions –> External Web Tools

转到工具 –> 选项 –> 项目和解决方案 –> 外部 Web 工具

  • Set the global PATH environment variable before the internal path, you can just use the arrows at the top-right to change the order.
  • 在内部路径之前设置全局 PATH 环境变量,您可以使用右上角的箭头更改顺序。

or

或者

  • First, find the Node.js installation you already have and use at the command line. By default, Node.js 0.12.7 installs to “C:\Program Files\nodejs”. Add this entry at the top to the path to the node.js directory to force Visual Studio to use that version instead
  • 首先,找到您已有的 Node.js 安装并在命令行中使用。默认情况下,Node.js 0.12.7 安装到“C:\Program Files\nodejs”。将此条目顶部添加到 node.js 目录的路径以强制 Visual Studio 使用该版本