Answer the question
In order to leave comments, you need to log in
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);
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question