0

I want to run a js script, that require some ts, and js file in my node project. I use this command node --experimental-modules src/scripts/myScript.js In my myScript.js i can't use import syntax only require is working. So i tend to use require only. But myScript.js is using multiple ts and js files that are using import syntax. Here is the error i got:

SyntaxError: Unexpected identifier
    at Module._compile (internal/modules/cjs/loader.js:721:23)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Module.require (internal/modules/cjs/loader.js:690:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at Object.<anonymous> (/Users/*****/Desktop/*****/l****/src/scripts/myScript.js:6:22)
    at Module._compile (internal/modules/cjs/loader.js:776:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
user2222
  • 444
  • 2
  • 8
  • 18
  • 1
    as far as I know, neither `import` nor typescript are supported by node. You'll need some sort of transpilation step. – junvar Nov 13 '19 at 22:14
  • 2
    Here already answered to this question https://stackoverflow.com/questions/45854169/how-can-i-use-an-es6-import-in-node – Jahongir Nov 13 '19 at 22:42
  • Explanation here: [Using import in node.js](https://stackoverflow.com/questions/58775491/node-js-javascript-calling-functions-from-javascript-file/58775856#58775856) – jfriend00 Nov 14 '19 at 00:31
  • run the script with npx ts-node was the solution – user2222 Feb 02 '20 at 15:19

0 Answers0