1

I am trying to build a typescript playground and to use ES module.

package.json

{
  "name": "playgroud",
  "version": "1.0.0",
  "description": "",
  "main": "index.ts",
  "type": "module",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "tsnd index.ts"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "ts-node-dev": "^1.1.6",
    "tslint": "^6.1.3",
    "typescript": "^4.2.4"
  },
  "dependencies": {
    "fp-ts": "^2.10.0"
  }
}

tsconfig.json

{
  "compilerOptions": {
    /* Visit https://aka.ms/tsconfig.json to read more about this file */
    "target": "ESNext",                                /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
    "module": "ESNext",                           /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */

    /* Strict Type-Checking Options */
    "strict": true,                                 /* Enable all strict type-checking options. */
    "strictNullChecks": true,                    /* Enable strict null checks. */
    "strictPropertyInitialization": true,        /* Enable strict checking of property initialization in classes. */
    "noImplicitThis": true,                      /* Raise error on 'this' expressions with an implied 'any' type. */
    "alwaysStrict": true,                        /* Parse in strict mode and emit "use strict" for each source file. */


    /* Module Resolution Options */
    "moduleResolution": "node",                  /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
    "esModuleInterop": true,                        /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */

    /* Advanced Options */
    "skipLibCheck": true,                           /* Skip type checking of declaration files. */
    "forceConsistentCasingInFileNames": true        /* Disallow inconsistently-cased references to the same file. */
  }
}

index.ts


import * as O from "fp-ts/Option";
import { pipe } from "fp-ts/function";



const fn = (x: number) : boolean => x > 3 ? true : false;

const a = O.of(3)

const b = O.ap(a)(O.some(fn))

const f = (a: string) => (b: number) => ({ a, b })

const res = pipe(
  O.some(f),
  O.ap(O.some("hello")),
  O.ap(O.some(12)),
)

console.log(res);

running yarn run start it shows an error:

[INFO] 20:14:46 Restarting: /home/developer/typescript/playgroud/tsconfig.json has been modified
(node:13329) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
/home/developer/typescript/playgroud/index.ts:1
import * as O from "fp-ts/Option";
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at Object.compileFunction (node:vm:355:18)
    at wrapSafe (node:internal/modules/cjs/loader:1022:15)
    at Module._compile (node:internal/modules/cjs/loader:1056:27)
    at Module._compile (/home/developer/typescript/playgroud/node_modules/source-map-support/source-map-support.js:547:25)
    at Module.m._compile (/tmp/ts-node-dev-hook-6308924620652321.js:69:33)
    at Module._extensions..js (node:internal/modules/cjs/loader:1121:10)
    at require.extensions.<computed> (/tmp/ts-node-dev-hook-6308924620652321.js:71:20)
    at Object.nodeDevHook [as .ts] (/home/developer/typescript/playgroud/node_modules/ts-node-dev/lib/hook.js:63:13)
    at Module.load (node:internal/modules/cjs/loader:972:32)
    at Function.Module._load (node:internal/modules/cjs/loader:813:14)
[ERROR] 20:14:46 SyntaxError: Cannot use import statement outside a module

What am I doing wrong? Here is the repo:

https://github.com/softshipper/ts-playground

1 Answer 1

1

ts-node-dev is using ts-node to compile TypeScript on the fly and it seems to have an issue here with ES Module outputs. I got your code working by going into tsconfig.json and changing module from ESNext to commonjs and then running npm start again, which printed:

> [email protected] start
> tsnd ./src/index.ts

[INFO] 13:26:37 ts-node-dev ver. 1.1.6 (using ts-node ver. 9.1.1, typescript ver. 4.2.4)
{ _tag: 'Some', value: { a: 'hello', b: 12 } }

So it would seem ts-node-dev and/or ts-node expect commonjs module outputs from the TypeScript compiler by default. I did find an option --loader ts-node/esm and adding that to your npm start in combination with module: ESNext in tsconfig.json led to different errors but did not solve the problem.

I tested this on a machine running node v15.11.0.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.