migrate-mongoose icon indicating copy to clipboard operation
migrate-mongoose copied to clipboard

I am getting "Cannot use import statement outside a module"

Open spmsupun opened this issue 4 years ago • 4 comments

When I try to run migrate up it says 'Cannot use import statement outside a module' image

spmsupun avatar Jun 15 '20 16:06 spmsupun

using const User = require('../models/user.js') instead of import { User } from '../models/user' should solve your issue

chris-aeviator avatar Jul 20 '20 09:07 chris-aeviator

The version of nodejs doesn't support EMCAScript modules So either:

  • upgrade to node 14+
  • run with node --experimental-modules node 8.9.0 +
  • use require as Chris suggested
  • also, require('@babel/register') if you require files that use import

nickhingston avatar Oct 17 '20 18:10 nickhingston

I was struggling with this too. If you are using babel, you can add a script to packages.json and run the script using npm run migrate-up. Here is what you can add to your packages.json under "scripts" section: "migrate-up": "./node_modules/.bin/babel-node ./node_modules/.bin/migrate up". This will make sure that everything is transpiled before running the migrate up command.

"scripts": {
    "dev": "nodemon src/server.js --exec babel-node --presets env",
    "build": "babel src -d dist --source-maps inline --copy-files",
    "migrate-up": "./node_modules/.bin/babel-node ./node_modules/.bin/migrate up"
  },

danline avatar Oct 12 '21 12:10 danline

@danline it is not working, i tried the same and getting this Screenshot 2022-06-26 at 4 28 06 PM my node version is v16.15.0, thanks

rizhk avatar Jun 26 '22 08:06 rizhk