Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SyntaxError: Unexpected token 'export' #1

Open
xmlking opened this issue Oct 27, 2019 · 7 comments
Open

SyntaxError: Unexpected token 'export' #1

xmlking opened this issue Oct 27, 2019 · 7 comments

Comments

@xmlking
Copy link

xmlking commented Oct 27, 2019

I am getting error, while trying to use with https://github.com/xmlking/ngx-starter-kit

        "build": {
          "builder": "ts-node-builder:build",
          "options": {
            "mainInOutput": "dist/out-tsc/apps/api/src/main.js",
            "tsconfig": "apps/api/tsconfig.app.json",
            "runAndBuild": true
          }
        },
/Users/sumo/Developer/Work/SPA/ngx-starter-kit/apps/api/src/environments/environment.ts:1
export const environment = {
^^^^^^

SyntaxError: Unexpected token 'export'
    at Module._compile (internal/modules/cjs/loader.js:892:18)
    at Module.m._compile (/Users/schintha/Developer/Work/SPA/ngx-starter-kit/node_modules/ts-node/src/index.ts:493:23)
    at Module._extensions..js (internal/modules/cjs/loader.js:973:10)
    at Object.require.extensions.<computed> [as .ts] (/Users/schintha/Developer/Work/SPA/ngx-starter-kit/node_modules/ts-node/src/index.ts:496:12)
    at Module.load (internal/modules/cjs/loader.js:812:32)
    at Function.Module._load (internal/modules/cjs/loader.js:724:14)
    at Module.require (internal/modules/cjs/loader.js:849:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at Object.<anonymous> (/Users/schintha/Developer/Work/SPA/ngx-starter-kit/apps/api/src/main.ts:1:1)
    at Module._compile (internal/modules/cjs/loader.js:956:30)
@abdoolly
Copy link
Owner

Thanks for raising an issue
let me check and get back to you.

@abdoolly
Copy link
Owner

I have been testing the starter kit , I am using the normal nx starter using the nx command line same config file but, it does not make any problems.

for you to be able to solve such a problem i will give you an understanding of how this command line works cause i think the problem is related to the tsconfig.json configuration you have.

the build command first runs the following command

tsc --build <the tsconfig path that was given in the config>

Then this was the build step if you made the runAndBuild to true it will run this command

node -r tsconfig-paths/register -r ts-node/register  <mainInOutput in the config>

if you test this same command you will find the same error happening to you there is something about the tsconfig but i don't know what it is exactly after fixing it i find other errors so I think you might want to try that and share with me here and we can collaborate on solving it.

Hope this helps in resolving the issue.
Please tell me i need to change something in the command that may fix the problem you are having.

@ashbrener
Copy link

@abdoolly I suspect you are using a far newer version of NodeJS. I am using v12.16.1 and have all the latest dependencies. I can confirm that in this environment I am experiencing the same issue as @xmlking.

Following the recommendations in this ts-node issue is the closest I have come to resolving this, but as yet still cannot get ts-node-builder to work.

@abdoolly
Copy link
Owner

abdoolly commented Mar 15, 2020

Hello @ashbrener I think the problem is not in the node version since my node version is v12.13.0
so , it's lower than yours but, I think I found out the problem is in your tsconfig.json file in which maybe it's compiling the typescript to something other than to "commonjs" make sure it's compiling to commonjs
also this is my outer tsconfig that the microservice projects in nrwl extends from.

{
  "compileOnSave": false,
  "compilerOptions": {
    "rootDir": ".",
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "importHelpers": true,
    "target": "es5",
    "module": "commonjs",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2017",
      // "dom"
    ],
    "skipLibCheck": true,
    "skipDefaultLibCheck": true,
    "baseUrl": ".",
    "paths": {
      "@test-nx/data": [
        "libs/data/src/index.ts"
      ]
    }
  },
  "exclude": [
    "node_modules",
    "tmp"
  ]
}

Note: when I tried to change the "module" to something other than commonjs an error happened similar to what happened with you but, it was complaining about the import statement.

import * as express from 'express';
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at Module._compile (internal/modules/cjs/loader.js:892:18)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)
    at Module.load (internal/modules/cjs/loader.js:812:32)
    at Function.Module._load (internal/modules/cjs/loader.js:724:14)
    at Function.Module.runMain (internal/modules/cjs/loader.js:1025:10)
    at internal/main/run_main_module.js:17:11

so, the tsconfig file module change should solve the problem for you I hope so.

@ashbrener
Copy link

I made those changes, now I get:

➜  my-app git:(master) ✗ node -r tsconfig-paths/register -r ts-node/register dist/out-tsc/apps/heartbeat/src/main.js
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /Users/me/Dev/my-app/dist/out-tsc/apps/heartbeat/src/main.js
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1174:13)
    at Module.load (internal/modules/cjs/loader.js:1002:32)
    at Function.Module._load (internal/modules/cjs/loader.js:901:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
    at internal/main/run_main_module.js:18:47```

@gperdomor
Copy link

I have similar problems with latest version of NX, which use module: esnext, the code compiles but not run, with commonjs works

@Zamiell
Copy link

Zamiell commented Aug 13, 2022

I was getting the same error as the OP, the fix for me was to specify the double --require flag as suggested by abdoolly above:

node --require "tsconfig-paths/register" --require "ts-node/register" "./dist/packages/server/src/main.js"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants