Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 574
Abdul Aziz Sabra Avatar asked Sep 06 '26 02:09

Abdul Aziz Sabra


1 Answers

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

like image 71
Borja Tur Avatar answered Sep 07 '26 15:09

Borja Tur



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!