3

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?

12
  • 1
    Node supports some parts of es6 but I don't believe import is one of them. I believe you still need babel or another transpiler for that. Commented Jun 10, 2016 at 16:47
  • ^ is the correct answer. What you should do is write your code in CommonJS style: let idb = require('idb'). Commented Jun 10, 2016 at 16:49
  • @Chev How can I use Babel with node.js to make this ? Commented Jun 10, 2016 at 16:55
  • 2
    Wait, require is undefined? Wat? You really running node.js or are you running your javascript in the browser? Is window defined? Commented Jun 10, 2016 at 16:56
  • 1
    @AbdulAzizSabra import doesn't work anywhere yet. Check out Browserify if you want to have modularized code that runs in the browser. Commented Jun 10, 2016 at 17:01

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

8 Comments

Thank you for your answer, but it is still not working with the same error.
"import" is used in lowercase maybe this cause the problem?
This is a sample use on my server.js => import express from 'express';
First I make This Command: npm install idp. and in my file I use this command : import idb from 'idp'; and I get this error : Uncaught SyntaxError: Unexpected token import
After install this "devDependencies" shoulde I make require for this packages? @Borjs Tur
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.