Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unexpected token import

Tags:

node.js

Using es2015 presets but still import doesn' work

import corsPrefetch from 'cors-prefetch-middleware';  
import imagesUpload from 'images-upload-middleware';

ERROR

import corsPrefetch from 'cors-prefetch-middleware';
^^^^^^
SyntaxError: Unexpected token import
like image 530
Yash Sharma Avatar asked May 24 '26 01:05

Yash Sharma


1 Answers

node.js doesn't support es6 module syntax yet. You have to use:

var corsPrefetch = require('cors-prefetch-middleware');

or use babel with the es2015 preset to transpile your es6 code back to es5:

"build": "babel --out-dir dist --presets=es2015
like image 81
Clintm Avatar answered May 26 '26 15:05

Clintm



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!