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?

Josh
  • 17,834
  • 7
  • 50
  • 68
  • 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. – CatDadCode Jun 10 '16 at 16:47
  • ^ is the correct answer. What you should do is write your code in CommonJS style: `let idb = require('idb')`. – Mike Cluck Jun 10 '16 at 16:49
  • @Chev How can I use Babel with node.js to make this ? – Abdul Aziz Sabra Jun 10 '16 at 16:55
  • @Mick C When I use your command. I get this Error : Uncaught ReferenceError: require is not defined – Abdul Aziz Sabra Jun 10 '16 at 16:55
  • They have some pretty decent instructions on [their site](https://babeljs.io/docs/learn-es2015/). – CatDadCode Jun 10 '16 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? – CatDadCode Jun 10 '16 at 16:56
  • @AbdulAzizSabra That's not possible. `require` is always defined in Node.js. How are you starting your app? It should be something like `node app.js` on your command line. Node.js is totally separate from the browser. – Mike Cluck Jun 10 '16 at 16:57
  • require work with server code, but not working with browser. So I need Import. – Abdul Aziz Sabra Jun 10 '16 at 17:00
  • 1
    @AbdulAzizSabra `import` [doesn't work *anywhere* yet.](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#Browser_compatibility) Check out [Browserify](http://browserify.org/) if you want to have modularized code that runs in the browser. – Mike Cluck Jun 10 '16 at 17:01
  • There is no require or import in the browser. You need to either A) Put a ` – CatDadCode Jun 10 '16 at 17:02
  • In this Course https://classroom.udacity.com/courses/ud899/lessons/6381510082/concepts/63774101910923 .The teacher use Import with Browser Code (to use indexeddb code with Promises). But he didnot speak how he did that. – Abdul Aziz Sabra Jun 10 '16 at 17:09
  • Does this answer your question? [Node.js - SyntaxError: Unexpected token import](https://stackoverflow.com/questions/39436322/node-js-syntaxerror-unexpected-token-import) – Heretic Monkey Feb 04 '20 at 17:14

1 Answers1

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

Borja Tur
  • 779
  • 1
  • 6
  • 20