Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unexpected token 'export' in Sequelize UUID

I'm trying to run Sequelize and is giving the following export error in the UUID module:

File: /uuid/dist/esm-browser/index.js:1

export { default as v1 } from './v1.js';
^^^^^^

SyntaxError: Unexpected token 'export'

What is the right way to solve this? I'm using node

like image 539
gelopes Avatar asked Jun 28 '20 05:06

gelopes


People also ask

How do I fix unexpected token export?

To solve the "Uncaught SyntaxError Unexpected token 'export'", refactor your code to use the CommonJS syntax, e.g. module. exports = {num}; instead of export num = 10; . Copied!

What is uncaught SyntaxError unexpected token 'export'?

Uncaught SyntaxError Unexpected token 'export'# The "Uncaught SyntaxError Unexpected token 'export'" error occurs for 2 main reasons: Using the ES6 Module syntax in a Node.js application without typeto modulein package.json.

Why am I getting Sequelize error when using ES6?

This error does not occur only when you have sequelize, it occurs for anyone using modules that use modern ES6 while your project is based on commonJS. e.g the UUID node module Here is a solution that might help, Just a point to note:

Why my project is not running after using Sequelize?

So anyone using sequelize module in their projects and when trying to run your project you we faced with this error as the project was booting up. This error does not occur only when you have sequelize, it occurs for anyone using modules that use modern ES6 while your project is based on commonJS. e.g the UUID node module

Is it possible to use UUID in Node JS?

Nope. UUID is distributed as commonjs by default. How do you import uuid? And provide please minimal reproduction. Sorry, something went wrong. which pointed to this file: What am i missing here? what is the root of problem ? uuidv4? uuid? esm-browser???? Sorry, something went wrong. Show how do you import uuid, give us a reproduction.


2 Answers

This is a problem in the https://github.com/uuidjs/uuid/ package where they don't support odd Node versions https://github.com/uuidjs/uuid/issues/466 .

Upgrade your Node version. I'm now on 14.4.0 and it works fine.

like image 164
James Avatar answered Oct 26 '22 00:10

James


So anyone using sequelize module in their projects and when trying to run your project you we faced with this error as the project was booting up. This error does not occur only when you have sequelize, it occurs for anyone using modules that use modern ES6 while your project is based on commonJS. e.g the UUID node module

export { default as v1 } from './v1.js';
^^^^^^

SyntaxError: Unexpected token 'export'

Here is a solution that might help, Just a point to note:

I experienced this error when i was deploying my app to digital ocean, i had the latest nodejs version v14.

In your project root directory

install babel and babel development dependencies

npm install --save-dev @babel/core @babel/cli @babel/preset-env @babel/node

this module helps you run modern es6 modules alongside commonJs code after installing babel create a .babelrc file in the root directory and add the following code

{   
"presets": [
 "@babel/preset-env"   
  ] 
}

This file when executed will tell babel how to compile ES6 modules found inside the node modules you have installed The final step is to execute the .babelrc in the node start execution pipeline file. Open package.json and edit your start script as below

"start" : "node --exec babel-node index.js"

or if using nodemon

"start" : "nodemon --exec babel-node index.js"

Make sure your .babelrc is created at the root directory where package.json, package.lock.json are created by npm init.

like image 1
Andrew Mititi Avatar answered Oct 26 '22 02:10

Andrew Mititi