Abdul Aziz Sabra
Abdul Aziz Sabra

Reputation: 71

Use Import command in Node.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: Unexpected token import

What should I do to solve this error?

Upvotes: 3

Views: 7377

Answers (1)

Borja Tur
Borja Tur

Reputation: 817

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. 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

Upvotes: 2

Related Questions