forest-express icon indicating copy to clipboard operation
forest-express copied to clipboard

SyntaxError: Cannot use import statement outside a module

Open fraywing opened this issue 4 years ago • 3 comments

Expected behavior

Allow importing of ESM modules within the require-all module import happens.

Actual behavior

When importing an ESM module using babel, if that module has another ESM module inside it, we get this error (which ultimately comes from require-all):

import { arrayToObject } from '../utils/arrayUtils';
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at wrapSafe (internal/modules/cjs/loader.js:1055:16)
    at Module._compile (internal/modules/cjs/loader.js:1103:27)
    at Module._compile (/Users/austin/Dev/RupieNetwork/node_modules/pirates/lib/index.js:99:24)
    at Module._extensions..js (internal/modules/cjs/loader.js:1159:10)
    at Object.newLoader [as .js] (/Users/austin/Dev/RupieNetwork/node_modules/pirates/lib/index.js:104:7)
    at Module.load (internal/modules/cjs/loader.js:988:32)
    at Function.Module._load (internal/modules/cjs/loader.js:896:14)
    at Module.require (internal/modules/cjs/loader.js:1028:19)
    at require (internal/modules/cjs/helpers.js:72:18)
    at Object.<anonymous> (/Users/austin/Dev/RupieNetwork/src/admin/models/gig.js:1:1)
    at Module._compile (internal/modules/cjs/loader.js:1139:30)
    at Module._compile (/Users/austin/Dev/RupieNetwork/node_modules/pirates/lib/index.js:99:24)
    at Module._extensions..js (internal/modules/cjs/loader.js:1159:10)
    at Object.newLoader [as .js] (/Users/austin/Dev/RupieNetwork/node_modules/pirates/lib/index.js:104:7)
    at Module.load (internal/modules/cjs/loader.js:988:32)
    at Function.Module._load (internal/modules/cjs/loader.js:896:14)
    at Module.require (internal/modules/cjs/loader.js:1028:19)
    at require (internal/modules/cjs/helpers.js:72:18)
    at /Users/austin/Dev/RupieNetwork/node_modules/require-all/index.js:56:46
    at Array.forEach (<anonymous>)
    at requireAll (/Users/austin/Dev/RupieNetwork/node_modules/require-all/index.js:34:9)
    at requireAllModels (/Users/austin/Dev/RupieNetwork/node_modules/forest-express/dist/index.js:95:7)
    at buildSchema (/Users/austin/Dev/RupieNetwork/node_modules/forest-express/dist/index.js:130:10)
    at Object.exports.init (/Users/austin/Dev/RupieNetwork/node_modules/forest-express/dist/index.js:232:3)
    at Object.init (/Users/austin/Dev/RupieNetwork/node_modules/forest-express-sequelize/dist/index.js:219:20)
    at new module.exports (/Users/austin/Dev/RupieNetwork/src/admin/middlewares/forestadmin.js:11:17)
    at resolve (/Users/austin/Dev/RupieNetwork/src/admin/app.js:62:24)
    at /Users/austin/Dev/RupieNetwork/node_modules/require-all/index.js:56:38
    at Array.forEach (<anonymous>)
    at requireAll (/Users/austin/Dev/RupieNetwork/node_modules/require-all/index.js:34:9)
    at /Users/austin/Dev/RupieNetwork/src/admin/app.js:59:3
    at Generator.next (<anonymous>)
    at asyncGeneratorStep (/Users/austin/Dev/RupieNetwork/src/admin/app.js:14:103)
    at _next (/Users/austin/Dev/RupieNetwork/src/admin/app.js:16:194)

Failure Logs

See above

Context

Importing ESM modules that contain nested ESM modules from within sequelize-express.

  • Package Version: ^5.6.0
  • Express Version: ~4.17.1

fraywing avatar Jan 21 '20 20:01 fraywing

Oh, and I'm using Node 13 -- which might have something to do with it?

fraywing avatar Jan 21 '20 22:01 fraywing

looks like PM2 had to release a fix similar: https://github.com/Unitech/pm2/issues/4540

fraywing avatar Jan 21 '20 22:01 fraywing

Hi @fraywing, apologies for the late answer. I apparently have the same issue using node v12.13.0.

It looks like Node.js does not natively support this syntax yet:

  • https://blog.logrocket.com/how-to-use-ecmascript-modules-with-node-js/
  • https://timonweb.com/tutorials/how-to-enable-ecmascript-6-imports-in-nodejs/

I guess you can pick one of those workaround, or continue to use CommonJS (as done by default in admin backend generated by the Lumber CLI).

Let me know if it helps.

arnaudbesnier avatar May 25 '20 15:05 arnaudbesnier