1

I am trying to run simple sample javascript script from the command line using npm run scriptName, I manage to run some random console.log, however when I try to import some constant or variable to the script file the console is trowing an error:

import {constant} from '../../src/myfiles'
       ^

SyntaxError: Unexpected token {

I tried removing the braces, naming the file as mjs. adding:"type": "module", in top of scripts in package.json.

nothing seems to be working and I am running out of ideas, so I ould like to ask you if someone knows what can be the issue here.

My script

import {constant} from '../../src/myfile'


export const translate = () => {
  console.log(constant)
}
translate()

file structure:

|-src
    |-restOfCode

|-scriptContainer
               |-scriptFolder
                            |-script

my package.json scripts look like:

 "scripts": {
   ...rest of scripts
    "export": "node scriptContainer/scriptFolder/script.js"


**edit**
**sadly changing to** 
"export": "node --experimental-modules scriptContainer/scriptFolder/script.js"

**does not solve the issue**

running 'npm run export' works when I just console.log some simple string.

thanks to whoever can give some light to this problem.

Jgarnie
  • 431
  • 6
  • 20

0 Answers0