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
19k7 gold badges55 silver badges73 bronze badges
asked Jun 10, 2016 at 16:46
Abdul Aziz Sabra
713 silver badges6 bronze badges
1 Answer 1
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"
}
}
- Install the same "devDependencies"
- Install nodemon globally "npm install nodemon -g"
- Configure your npm start command with the same of my package.json changing "server.js" with your entry file in the app
- Run "npm start"
Then you can use import syntax
answered Jun 10, 2016 at 17:16
Borja Tur
8171 gold badge8 silver badges21 bronze badges
Sign up to request clarification or add additional context in comments.
8 Comments
Abdul Aziz Sabra
Thank you for your answer, but it is still not working with the same error.
Borja Tur
"import" is used in lowercase maybe this cause the problem?
Borja Tur
This is a sample use on my server.js => import express from 'express';
Abdul Aziz Sabra
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
Abdul Aziz Sabra
After install this "devDependencies" shoulde I make require for this packages? @Borjs Tur
|
lang-js
importis one of them. I believe you still need babel or another transpiler for that.let idb = require('idb').requireis undefined? Wat? You really running node.js or are you running your javascript in the browser? Iswindowdefined?importdoesn't work anywhere yet. Check out Browserify if you want to have modularized code that runs in the browser.