{"id":106,"hash":"1c9d33c604ad3b3fa965163d59a2197a634a17b3c2940bbf7e72a4d637c9c26d","pattern":"Node.js - SyntaxError: Unexpected token import","full_message":"I don't understand what is wrong.\nNode v5.6.0\nNPM v3.10.6\n\nThe code:\n\nfunction (exports, require, module, __filename, __dirname) {\n    import express from 'express'\n};\n\nThe error:\n\nSyntaxError: Unexpected token import\n    at exports.runInThisContext (vm.js:53:16)\n    at Module._compile (module.js:387:25)\n    at Object.Module._extensions..js (module.js:422:10)\n    at Module.load (module.js:357:32)\n    at Function.Module._load (module.js:314:12)\n    at Function.Module.runMain (module.js:447:10)\n    at startup (node.js:140:18)\n    at node.js:1001:3","ecosystem":"npm","package_name":"node.js","package_version":null,"solution":"Node 13+ Since Node 13, you can use either the .mjs extension, or set {\"type\": \"module\"} in your package.json. You don't need to use the --experimental-modules flag. Modules is now marked as stable in node.js\n\nNode 12 Since Node 12, you can use either the .mjs extension, or set \"type\": \"module\" in your package.json. And you need to run node with the --experimental-modules flag.\n\nNode 9 In Node 9, it is enabled behind a flag, and uses the .mjs extension.\n\nnode --experimental-modules my-app.mjs\n\nWhile import is indeed part of ES6, it is unfortunately not yet supported in NodeJS by default, and has only very recently landed support in browsers.\n\nSee browser compat table on MDN and this Node issue.\n\nFrom James M Snell's Update on ES6 Modules in Node.js (February 2017):\n\nWork is in progress but it is going to take some time — We’re currently looking at around a year at least.\n\nUntil support shows up natively (now marked stable in Node 13+), you'll have to continue using classic require statements:\n\nconst express = require(\"express\");\n\nIf you really want to use new ES6/7 features in NodeJS, you can compile it using Babel. Here's an example server.","confidence":0.95,"source":"stackoverflow","source_url":"https://stackoverflow.com/questions/39436322/node-js-syntaxerror-unexpected-token-import","votes":532,"created_at":"2026-04-19T04:41:20.061243+00:00","updated_at":"2026-04-19T04:51:01.809334+00:00"}