angular / angular-cli

CLI tool for Angular
https://cli.angular.io
MIT License
26.73k stars 11.98k forks source link

Cannot run ts-node inside root folder of Angular project #17788

Closed fireflysemantics closed 4 years ago

fireflysemantics commented 4 years ago

Cannot run ts-node inside Angular project root folder.

This error happens:

import { parse } from 'papaparse';
       ^

SyntaxError: Unexpected token {

It's something with the tsconfig.json file most likely. I already reported the same issue against ng-packagr.

Outside of Angular the code runs fine, even with any tsconfig at all.

petebacondarwin commented 4 years ago

This is because the module in the standard tsconfig.json that comes from a new CLI application is set to esnext.

E.g.

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "downlevelIteration": true,
    "experimentalDecorators": true,
    "module": "esnext",
    "moduleResolution": "node",
    "importHelpers": true,
    "target": "es2015",
    "lib": [
      "es2018",
      "dom"
    ]
  },
  "angularCompilerOptions": {
    "fullTemplateTypeCheck": true,
    "strictInjectionParameters": true
  }
}

If you set it to commonjs then ts-node will run the code fine. E.g.

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "downlevelIteration": true,
    "experimentalDecorators": true,
    "module": "commonjs",
    "moduleResolution": "node",
    "importHelpers": true,
    "target": "es2015",
    "lib": [
      "es2018",
      "dom"
    ]
  },
  "angularCompilerOptions": {
    "fullTemplateTypeCheck": true,
    "strictInjectionParameters": true
  }
}

If you don't want to change the default tsconfig.json file then you can tell ts-node to use a different one with the --project flag.

This is not a problem with the Angular framework. If anything you should take it up with the CLI team, but I suspect that this issue will be closed with "works as expected".

alan-agius4 commented 4 years ago

Hi @fireflysemantics, as @petebacondarwin explained this is working as expected.

ES modules are not supported by default in Node versions < 14, thus you'd need to create a new tsconfig that uses CommonJS as module and provide it to ts-node via the --project option.

angular-automatic-lock-bot[bot] commented 4 years ago

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.