Skip to content

--experimental-modules flag not working #27441

Closed

Description

@TidyIQ

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)

Activity

  1. targos commented on Apr 27, 2019

    @targos
    Member

    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

  2. added
    question
    esm
    on Apr 27, 2019
  3. TidyIQ commented on Apr 27, 2019

    @TidyIQ
    Author

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

  4. reopened this on Apr 27, 2019
  5. targos commented on Apr 27, 2019

    @targos
    Member

    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.

  6. TidyIQ commented on Apr 27, 2019

    @TidyIQ
    Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    esmquestion

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions