我试图在Node.js中获得ES6导入的窍门,并试图使用本示例中提供的语法:

Cheatsheet链接

我正在查看支持表,但我无法找到支持新的导入语句的版本(我尝试寻找文本import/require)。我目前正在运行Node.js 8.1.2,也相信由于备考表引用的是.js文件,它应该与.js文件一起工作。

当我运行代码时(摘自备忘单的第一个例子):

import { square, diag } from 'lib';

我得到了错误:

SyntaxError:意外的令牌导入。

引用库,我试图导入:

//------ 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));
}

我错过了什么,我如何才能让节点识别我的导入语句?


当前回答

我只是想在JavaScript文件中使用导入和导出。

每个人都说这不可能。但是,截至2018年5月,可以在普通的Node.js中使用以上内容,而不需要任何像Babel等模块。

这里有一个简单的方法。

创建下面的文件,运行并自己查看输出。

别忘了看下面的解释。

文件myfile.mjs

function myFunc() {
    console.log("Hello from myFunc")
}

export default myFunc;

文件index.mjs

import myFunc from "./myfile.mjs"  // Simply using "./myfile" may not work in all resolvers

myFunc();

Run

node  --experimental-modules  index.mjs

输出

(node:12020) ExperimentalWarning: The ESM module loader is experimental.

Hello from myFunc

解释:

Since it is experimental modules, .js files are named .mjs files While running you will add --experimental-modules to the node index.mjs While running with experimental modules in the output you will see: "(node:12020) ExperimentalWarning: The ESM module loader is experimental. " I have the current release of Node.js, so if I run node --version, it gives me "v10.3.0", though the LTE/stable/recommended version is 8.11.2 LTS. Someday in the future, you could use .js instead of .mjs, as the features become stable instead of Experimental. More on experimental features, see: https://nodejs.org/api/esm.html

其他回答

我不知道这是否适用于你的情况,但我正在运行一个Express.js服务器:

nodemon --inspect ./index.js --exec babel-node --presets es2015,stage-2

这使我能够导入和使用扩展操作符,即使我只使用Node.js版本8。

您需要安装babel-cli、babel-预置-es2015和babel-预置-stage-2来完成我正在做的工作。

使用Node.js v12.2.0,我可以像这样导入所有标准模块:

import * as Http from 'http'
import * as Fs from 'fs'
import * as Path from 'path'
import * as Readline from 'readline'
import * as Os from 'os'

与我之前做的相比:

const
  Http = require('http')
  ,Fs = require('fs')
  ,Path = require('path')
  ,Readline = require('readline')
  ,Os = require('os')

任何ECMAScript模块都可以导入,而不需要使用.mjs扩展名,只要它的包中有这个字段。json文件:

"type": "module"

所以一定要放一个这样的包。Json文件在同一个文件夹的模块,你正在做。

要导入没有更新ECMAScript模块支持的模块,你可以这样做:

// Implement the old require function
import { createRequire } from 'module'
const require = createRequire(import.meta.url)

// Now you can require whatever
const
  WebSocket = require('ws')
  ,Mime = require('mime-types')
  ,Chokidar = require('chokidar')

当然,不要忘记这是使用模块导入实际运行脚本所需要的(在v13.2之后不需要了):

node --experimental-modules my-script-that-use-import.js

父文件夹需要这个包。Json文件,该脚本不抱怨导入语法:

{
  "type": "module"
}

如果您想要使用的模块还没有更新到支持使用import语法导入,那么您除了使用require别无选择(但对于上面的解决方案,这不是问题)。

我还想分享这段代码,它实现了模块中缺少的__filename和__dirname常量:

import {fileURLToPath} from 'url'
import {dirname} from 'path'
const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)

Use:

  "devDependencies": {
    "@babel/core": "^7.2.0",
    "@babel/preset-env": "^7.2.0",
    "@babel/register": "^7.0.0"
  }

文件 .babelrc

{
  "presets": ["@babel/preset-env"]
}

Node.js应用的入口点:

require("@babel/register")({})

// Import the rest of our application.
module.exports = require('./index.js')

参见如何在Node.js中启用ES6导入

Node.js已经包含了对ES6支持的实验性支持。 更多信息请访问:https://nodejs.org/docs/latest-v13.x/api/esm.html#esm_enabling。

TLDR;

Node.js >= v13

这在Node.js 13及以上版本中非常简单。你需要:

保存扩展名为.mjs或 在最近的package.json中添加{"type": "module"}。

您只需要执行上面的其中一项,就可以使用ECMAScript模块。

Node.js <= v12

如果你使用的是Node.js 9.6 - 12版本,保存带有.mjs扩展名的ES6模块文件,并像这样运行它:

node --experimental-modules my-app.mjs

你可以试试esm。

这里有一些介绍:esm