Our great sponsors
-
SurveyJS
Open-Source JSON Form Builder to Create Dynamic Forms Right in Your App. With SurveyJS form UI libraries, you can build and style forms in a fully-integrated drag & drop form builder, render them in your JS app, and store form submission data in any backend, inc. PHP, ASP.NET Core, and Node.js.
By default, Node assumes you're targeting CJS. This error is a node error telling you that "import ..." is a syntax error when you're configured for running CJS. Based on your description, I suspect you have "module": "es2015" or higher in your tsconfig. This tells typescript to output files that are compatible with ESM. At this point, if you change this to "module": "commonjs", you'd likely have a working set of code (this is my recommended approach). But... you took the experimental long route 😀. By adding "type": "module", you told node to run it as if it were ESM. Now you have Node and TS targeting ESM, but ts-node doesn't have an easy way of being compatible with ESM. Switching the loader to use ts-node/esm is then what put all 3 tools into the same ecosystem configuration. Note that this config is a bit fragile, the hooks are experimental, so Node doesn't follow semver with them. Minor version changes in node may break compatibility. Which leads into...
Related posts
- "SyntaxError: Cannot use import statement outside a module" trying to run Mathigon/Studio
- An Introduction to Deno: Is It Better Than Node.js?
- Newbie - Converting TS files to JS
- How to get started with TS as a developer with 2 years of JS (React, RN) development experience?
- TypeScript Without Transpilation