cevek / ttypescript

Over TypeScript tool to use custom transformers in the tsconfig.json
1.53k stars 56 forks source link

getting "SyntaxError: Cannot use import statement outside a module" error using ts-node with -C typescript option #71

Closed opensas closed 4 years ago

opensas commented 4 years ago

I was using ts-node with tsconfig-paths like this:

"dev:ts-node": "ts-node -r tsconfig-paths/register src/start.ts",

and everything worked fine. Then I tried to use it with ttypescript to get rid of the tsconfig-paths dependency, like it's shown here: https://github.com/cevek/ttypescript#ts-node:

    "dev:ts-node": "ts-node -C ttypescript src/start.ts",

But when I try it I get the following error:

C:\data\devel\apps\sgte-it\coordinacion\juridicos\wspjn\src (master -> origin)
λ npm run dev:ts-node

> wspjn@0.0.1 dev:ts-node C:\data\devel\apps\sgte-it\coordinacion\juridicos\wspjn
> ts-node -C ttypescript src/start.ts

C:\data\devel\apps\sgte-it\coordinacion\juridicos\wspjn\src\app.ts:1
import express from 'express'
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at Module._compile (internal/modules/cjs/loader.js:881:18)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:962:10)
    at Module.load (internal/modules/cjs/loader.js:798:32)
    at Function.Module._load (internal/modules/cjs/loader.js:711:12)
    at Module.require (internal/modules/cjs/loader.js:838:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at Object.<anonymous> (C:\data\devel\apps\sgte-it\coordinacion\juridicos\wspjn\src\start.ts:5:1)
    at Module._compile (internal/modules/cjs/loader.js:945:30)
    at Module.m._compile (C:\data\devel\apps\sgte-it\coordinacion\juridicos\wspjn\node_modules\ts-node\src\index.ts:530:23)
    at Module._extensions..js (internal/modules/cjs/loader.js:962:10)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! wspjn@0.0.1 dev:ts-node: `ts-node -C ttypescript src/start.ts`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the wspjn@0.0.1 dev:ts-node script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\sas\AppData\Roaming\npm-cache\_logs\2019-11-28T13_28_58_938Z-debug.log

This is the file app.ts

import express from 'express'
const app = express()   // Create a new express application instance
[...]

I'm using ttsc to run npm run prod and they work ok.

For the moment I'm keeping tsconfig-paths, but I'd like to get rid of it

thanks a lot

nonara commented 4 years ago

I ran into this issue during porting some of this code for ts-patch. The problem is that tts walks up the node modules chain to look for ts-node. If it finds it, it does not call ts-node.register. The problem is, there are cases where ts-node can be loaded without having had register called.

In this case, during createProgram, when it tries to load your Typescript based transformer, it will fail because it cannot interpret the TS in the transformer.

It sounds like that's what's happening. I don't maintain tts, but you're welcome to give ts-patch a try. If you run into any problems, please don't hesitate to file an issue!

cevek commented 4 years ago

@opensas Can you please provide repro project. I have tried your example, and it works for me

opensas commented 4 years ago

Sure, I tried to reproduce a pretty minimal example here: https://gitlab.com/opensas/tts-node_test

package.json

{
  "name": "tmp",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "ts-node": "ts-node -r tsconfig-paths/register src/start.ts",
    "tts-node": "ts-node -C ttypescript src/start.ts"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@types/express": "^4.17.2",
    "ts-node": "^8.5.4",
    "ts-transformer": "^0.1.5",
    "ts-transformer-imports": "^0.4.3",
    "tsconfig-paths": "^3.9.0",
    "ttypescript": "^1.5.8",
    "typescript": "^3.7.2"
  },
  "dependencies": {
    "express": "^4.17.1"
  }
}

tsconfig.json

{
  "compilerOptions": {
    "baseUrl": "./src",                      
    // "paths": {                               
    //   "@*": ["./*"]
    // },
    "plugins": [
      { "transform": "ts-transformer-imports" }
    ],
    "esModuleInterop": true
  }
}

src/start.ts

import app from './app'

app.get('/ping', (req: any, res: any) => {
  return res.send('ok!')
})

app.listen(8080, () =>
  console.log('listening on localhost:8080/ping')
)

src/app.ts

import express from 'express'

export default express()
$ npm -v
6.11.3

$ node -v
v12.11.1
cevek commented 4 years ago

@opensas The problem lies within ts-transformer-imports see this issues for more info https://github.com/cevek/ttypescript/issues/50 https://github.com/cevek/ttypescript/issues/43

so, just try another path mapper transformer like https://github.com/zerkalica/zerollup/tree/master/packages/ts-transform-paths

opensas commented 4 years ago

I can confirm that it works ok, I just did the following:

npm i -D @zerollup/ts-transform-paths

and then

tsconfig.json

{
  "compilerOptions": {
    "outDir": "build",
    "baseUrl": "src",
    "paths": {
      "@*": ["./*"]
    },
    // "plugins": [
    //   { "transform": "ts-transformer-imports" }
    // ],
    "plugins": [
      { "transform": "@zerollup/ts-transform-paths", "exclude": ["*"] }
    ],
    "esModuleInterop": true
  }
}

and now ts-node -C ttypescript src/start.ts works ok