我试图在我的项目中运行一些ES6代码,但我得到了一个意外的令牌导出错误。

export class MyClass {
  constructor() {
    console.log("es6");
  }
}

当前回答

通常import不能在.js扩展名中工作,因为默认情况下js意味着javascript的cjs版本。如果你想要es6特性,你需要将.js扩展名重命名为.mjs扩展名

parent.mjs

export default class Car {
   constructor(brand) {
   this.carname = brand;
}
 present() {
   return 'I have a ' + this.carname;
  }
}

child.mjs

import Car from './parent.mjs'
export default class Model extends Car {
constructor(brand, mod , country) {
  super(brand);
  this.model = mod;
  this.country = country;
}
show() {
  return this.present() + ', it is a ' + this.model + "i am from " + 
  this.country;
  }
}

index . html

<!DOCTYPE html>
 <html lang="en" class="no-js">
  <head>
  <meta charset="utf-8">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, 
shrink-to-fit=no">
  <title>Quick Start Pack Template</title>
 </head>
 <div class="demo"></div>
 <script type="module">
   import Model from './child.mjs'
   let value = new Model("Ford", "Mustang", "bangladesh")
   document.querySelector(".demo").innerHTML = value.show()
 </script>
 </body>
 </html>

最后在活动服务器上运行此代码

其他回答

我通过制作一个入口点文件来解决这个问题。

// index.js
require = require('esm')(module)
module.exports = require('./app.js')

我在app.js内外导入的任何文件都可以使用导入/导出 现在你可以像node index.js一样运行它

注意:如果app.js使用export default,在使用入口点文件时,这将变成require('./app.js').default。

2022年更新

您正在使用EcmaScript模块(ESM或'ES6模块')语法,但您的环境不支持它。

v14.13.0之前的NodeJS版本不支持ESM(导出关键字语法),并使用CommonJS Modules (module. js Modules)。导出属性语法)。NodeJS v14.13.0及更新版本支持ESM,但必须先启用它。

解决方案:

If you are using NodeJS v14.13.0 or newer (which does support ESM) you can enable it by setting "type":"module" in your project package.json Refactor with CommonJS Module syntax (for older versions of NodeJS) Consider using TypeScript alongside ts-node or ts-node-dev npm packages (for instant transpilation at development time) and write TypeScript in .ts files Transpile ESM to CommonJS using esbuild (esbuild package on npm) configured to transpile your ES6 javascript to a CommonJS target supported by your environment. (babel is no longer recommended)

在最新版本的Nodejs (v17?)中,您可以通过使用.mjs文件扩展名来使用顶级的“import”,“async”,“await”-而不是transpiling或变通方法。

   // > node my.mjs
   
   import {MyClass} from 'https://someurl'
   async func () {
     // return some promise
   }
   await func ()

如果遇到此错误,也可能与将JavaScript文件包含到html页面的方式有关。在加载模块时,必须显式地声明这些文件。这里有一个例子:

//module.js:
function foo(){
   return "foo";
}

var bar = "bar";

export { foo, bar };

当你像这样包含脚本时:

<script src="module.js"></script>

你会得到错误:

Uncaught SyntaxError:意外的令牌导出

你需要包含一个type属性设置为"module"的文件:

<script type="module" src="module.js"></script>

然后它应该会像预期的那样工作,你已经准备好在另一个模块中导入你的模块了:

import { foo, bar } from  "./module.js";

console.log( foo() );
console.log( bar );

安装babel包@babel/core和@babel/preset,这将把ES6转换为commonjs目标,因为node js不直接理解ES6目标

npm install --save-dev @babel/core @babel/preset-env

然后,您需要在项目的根目录中创建一个名为.babelrc的配置文件,并将这段代码添加到其中。

[“@babel/preset-env”]