Node: --experimental-modules flag not working

Created on 27 Apr 2019  路  4Comments  路  Source: nodejs/node

Version: 10.15.0
Platform: Windows 10 64bit

My package.json:

{
  "name": "server",
  "version": "1.0.0",
  "description": "API server",
  "private": true,
  "type": "module",
  "scripts": {
    "start": "node --experimental-modules out/index.js",
    "type-check": "tsc"
  },
  "author": "Jake",
  "license": "ISC",
  "devDependencies": {
    "@babel/core": "^7.4.3",
    "@babel/node": "^7.2.2",
    "@babel/preset-env": "^7.4.3",
    "@babel/register": "^7.4.0",
    "@types/bcrypt": "^3.0.0",
    "@types/bluebird": "^3.5.26",
    "@types/cors": "^2.8.5",
    "@types/dotenv": "^6.1.1",
    "@types/express": "^4.16.1",
    "@types/graphql": "^14.2.0",
    "@types/graphql-resolvers": "^0.2.0",
    "@types/ioredis": "^4.0.10",
    "@types/jsonwebtoken": "^8.3.2",
    "@types/node": "^11.13.7",
    "@types/nodemailer": "^4.6.7",
    "@types/pg": "^7.4.14",
    "@types/validator": "^10.11.0",
    "nodemon": "^1.18.11",
    "typescript": "^3.4.5"
  },
  "dependencies": {
    "apollo-server": "^2.4.8",
    "apollo-server-express": "^2.4.8",
    "bcrypt": "^3.0.6",
    "cors": "^2.8.5",
    "dotenv": "^7.0.0",
    "express": "^4.16.4",
    "graphql": "^14.2.1",
    "graphql-redis-subscriptions": "^2.1.0",
    "graphql-resolvers": "^0.3.2",
    "ioredis": "^4.9.0",
    "jsonwebtoken": "^8.5.1",
    "nodemailer": "^6.1.0",
    "pg": "^7.9.0",
    "sequelize": "^5.5.0"
  }
}

When I run npm run start or node --experimental-modules out/index.js I receive the error:

(node:41396) ExperimentalWarning: The ESM module loader is experimental.
C:\...\out\index.js:1
(function (exports, require, module, __filename, __dirname) { import dotenv from "dotenv";
                                                                     ^^^^^^

SyntaxError: Unexpected identifier
    at new Script (vm.js:79:7)
    at createScript (vm.js:251:10)
    at Proxy.runInThisContext (vm.js:303:10)
    at Module._compile (internal/modules/cjs/loader.js:657:28)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at createDynamicModule (internal/modules/esm/translators.js:56:15)
    at setExecutor (internal/modules/esm/create_dynamic_module.js:49:23)
ES Modules question

Most helpful comment

Sorry, I overlooked it. The implementation of ESM in Node.js changed in version 12.0.0. "type": "module" wasn't supported in 10.x.

All 4 comments

To make Node.js interpret your scripts as ESM instead of CommonJS, you have to either add a "type": "module" field in your package.json or use the .mjs extension: https://nodejs.org/dist/latest-v12.x/docs/api/esm.html#esm_enabling

I have "type": "module" in my package.json. The error still occurs. Can you please open this again?

Sorry, I overlooked it. The implementation of ESM in Node.js changed in version 12.0.0. "type": "module" wasn't supported in 10.x.

Yep that fixed it. Thanks. I had the LTS version of Node installed, not the current version. Updated and now it's working.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

vsemozhetbyt picture vsemozhetbyt  路  3Comments

akdor1154 picture akdor1154  路  3Comments

cong88 picture cong88  路  3Comments

mcollina picture mcollina  路  3Comments

filipesilvaa picture filipesilvaa  路  3Comments