K
K
kr_ilya2020-03-28 09:19:31
Node.js
kr_ilya, 2020-03-28 09:19:31

How to fix this nodejs, typescript error?

In order: 1. Downloaded this repository as a
zip archive . 2. Unzipped, uploaded the files to the server, executed in the /root/autoinst directory . 3. Being in the directory /root/autoinst executed I got an error:
npm i
node examples/upload-story.example.ts

(node:28667) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
/root/autoinst/examples/upload-story.example.ts:1
import { IgApiClient } from '../src';
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at wrapSafe (internal/modules/cjs/loader.js:1063:16)
    at Module._compile (internal/modules/cjs/loader.js:1111:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1167:10)
    at Module.load (internal/modules/cjs/loader.js:996:32)
    at Function.Module._load (internal/modules/cjs/loader.js:896:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47

4. Added packege.json to the root "type": "module"
5. Repeated the command from step 3, got an error
(node:28759) ExperimentalWarning: The ESM module loader is experimental.
internal/modules/run_main.js:54
    internalBinding('errors').triggerUncaughtException(
                              ^

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /root/autoinst/examples/upload-story.example.ts
    at Loader.defaultGetFormat [as _getFormat] (internal/modules/esm/get_format.js:65:15)
    at Loader.getFormat (internal/modules/esm/loader.js:110:42)
    at Loader.getModuleJob (internal/modules/esm/loader.js:241:31)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
    at async Loader.import (internal/modules/esm/loader.js:175:17) {
  code: 'ERR_UNKNOWN_FILE_EXTENSION'
}


Node version v13.12.0
I googled it and didn't find a working solution. What can be done?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Belyaev, 2020-03-28
@kr_ilya

The node does not know how to typescript
Option 1: compile
install: npm i typescriptand run $(npm bin)/tsc examplesnaturally need tsconfig.json
Option 2: ts-node
install: npm i typescript ts-nodeand run

$(npm bin)/ts-node examples/upload-story.example.ts

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question