M
M
mimicria2018-09-16 14:22:28
JavaScript
mimicria, 2018-09-16 14:22:28

Unexpected identifier error when running file from console?

I am writing a cli utility, when I call a file from the command line, an error appears:
import program from 'commander';
^^^^^^^
SyntaxError: Unexpected identifier
at new Script (vm.js:74:7)
at createScript (vm.js:246:10)
at Object.runInThisContext (vm.js:298: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 Function. Module.runMain (internal/modules/cjs/loader.js:742:12)
at startup (internal/bootstrap/node.js:266:19)
The file code itself:

#!/usr/bin/env node

import program from 'commander';
import getWeather from '../weather';

export default program
  .version('0.1.0')
  .description('Return weather by ip')
  .arguments('<firstConfig>')
  .option('-f, --format [type]', 'Parse format', 'default')
  .action(async (firstConfig, type) => console.log(getWeather(firstConfig, type.format)))
  .parse(process.argv);

dependencies commander is installed everything is OK
"dependencies": {
"commander": "^2.18.0",
"lodash": "^4.17.11",
"nock": "^10.0.0",
"node-fetch": "^2.2.0"
}
In other projects, similar files started normally, I don't understand what the problem is

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Belyaev, 2018-09-16
@mimicria

node.js does not launch ES6 modules out of the box (only with a flag, but when launched through the .js executable file, the flag is not set)
Use the loader via esm

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question