2
  • My nuxt project uses serverMiddleware defined in nuxt.config.js as serverMiddleware: ['~/api']
  • All the files inside my api directory use import and export statements and work perfectly when I run npm run dev
  • I also have a seeders directory which contains a bunch of database seeder files with each one having import and export statements
  • When I run it from package.json with "seed:dev": "cross-env NODE_ENV=development node ./api/db/seeders",

I get the following error

(node:12212) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
/Users/zup/Desktop/code/ACTIVE/ch_v3_final/api/db/seeders/index.js:1
import feeds from './feeds-data'
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at wrapSafe (internal/modules/cjs/loader.js:915:16)
    at Module._compile (internal/modules/cjs/loader.js:963:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
    at Module.load (internal/modules/cjs/loader.js:863:32)
    at Function.Module._load (internal/modules/cjs/loader.js:708:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)
    at internal/main/run_main_module.js:17:47
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! ch@1.0.0 seed:dev: `cross-env NODE_ENV=development node ./api/db/seeders`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the ch@1.0.0 seed:dev script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/zup/.npm/_logs/2021-03-21T16_38_58_766Z-debug.log

How do I run the database seeders which use import export statements the way nuxt runs my other files?

kissu
  • 6,476
  • 5
  • 15
  • 33
PirateApp
  • 4,149
  • 2
  • 34
  • 51
  • you can use `require` syntax here. `const feed = require('./feeds-data');` – Vishnu Mar 23 '21 at 17:19
  • it refers to other files like db, which is full of import and export statements, it still gives the error, db file contains core database logic which is part of the application – PirateApp Mar 23 '21 at 17:28

1 Answers1

1

What is your Node.js version ?

Depending of it, you have several options as detailed here: https://stackoverflow.com/a/45854500/8816585

The most simple is still to have a "type": "module" in your package.json as the error tells you !

The latest LTS is: 14.16.0 and it may not be supported by all the platforms, so beware.

PS: it's a bit more simple in v15 if I'm not mistaken but this version is still experimental.

TLDR: nothing related to Nuxt in any way. ^^

kissu
  • 6,476
  • 5
  • 15
  • 33
  • this is related to nuxt because because the files that are a part of nuxt run without issues, for example here is a demo repo i setup https://github.com/slidenerd/nuxt-ws-content-bug everything works well here when you run npm run dev but if i have seeders in my database that i run from package.json they dont work with import and export, problem is my seeders refer to other files that run with nuxt (with no problems on import export statements) – PirateApp Mar 24 '21 at 05:48
  • The thing that is buggy is `cross-env NODE_ENV=development node ./api/db/seeders`, hence the `node` in it and the error which is 100% related to node. All the remaining code is working great. So if your using Nuxt, hosting on AWS, having a cloudinary or hashing thanks to bcrypt, it's not relevant here (also, Nuxt is working great). – kissu Mar 25 '21 at 07:53