在Node.js中使用Import命令 [英] Use Import command in Node.js

查看:794
本文介绍了在Node.js中使用Import命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我安装了此节点包 https:// github。 com / jakearchibald / indexeddb-promised / blob / master / lib / idb.js ,我正在尝试使用它。

I have this node package installed https://github.com/jakearchibald/indexeddb-promised/blob/master/lib/idb.js, and am trying to use it.

我正在尝试使用此命令:

I am trying to use this command:

(Import idb from 'idb')

不幸的是,我收到此错误:

Unfortunately, I get this error:


Uncaught SyntaxError:意外的令牌导入

Uncaught SyntaxError: Unexpected token import

我该怎么做才能解决这个错误?

What should I do to solve this error?

推荐答案

您可以使用babel以ES6语法将代码以透明的方式转换为ES5,以便进行开发。这是我在演示应用中的package.json的一部分

You can use babel to transpile your code in ES6 syntax to ES5 in a transparent way for your develop. This is a part of my package.json in a demo app

 {
  "name": "**********",
  "version": "1.0.0",
  "description": "",
  "main": "server.js",
  "scripts": {
    "start": "nodemon server.js --exec babel-node --presets es2015,stage-2"
  },
  "author": "Borja Tur",
  "license": "ISC",
  "dependencies": {
    "bcrypt-nodejs": "0.0.3",
    "body-parser": "^1.15.1",
    "express": "^4.13.4",
    "jsonwebtoken": "^7.0.0",
    "mongoose": "^4.4.19",
    "morgan": "^1.7.0"
  },
  "devDependencies": {
    "babel": "^6.5.2",
    "babel-cli": "^6.9.0",
    "babel-preset-es2015": "^6.9.0",
    "babel-preset-stage-2": "^6.5.0"
  }
}




  1. 安装相同的 devDependencies

  2. 全局安装nodemon npm install nodemon -g

  3. 配置你的npm 开始命令与我的package.json相同,将 server.js 更改为应用程序中的输入文件

  4. 运行 npm start

  1. Install the same "devDependencies"
  2. Install nodemon globally "npm install nodemon -g"
  3. Configure your npm start command with the same of my package.json changing "server.js" with your entry file in the app
  4. Run "npm start"

然后你可以使用导入语法

Then you can use import syntax

这篇关于在Node.js中使用Import命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆