Skip to content
This repository has been archived by the owner on Jan 13, 2024. It is now read-only.

Compilation works but execution failed "SyntaxError: Cannot use import statement outside a module" #895

Closed
Paul75 opened this issue Apr 27, 2020 · 5 comments

Comments

@Paul75
Copy link

Paul75 commented Apr 27, 2020

Hello,

What happened:
Compilation good but not start. Got error.

In packages.json add :

"bin": "dist/app.js",
...
"pkg": {
    "assets": [
      ".env",
      "node_modules/acorn/**/*",
      "node_modules/app-root-path/**/*",
      "node_modules/axios/**/*",
      "node_modules/bcrypt/**/*",
      "node_modules/chalk/**/*",
      "node_modules/class-transformer/**/*",
      "node_modules/class-validator/**/*",
      "node_modules/commander/**/*",
      "node_modules/compression/**/*",
      "node_modules/copyfiles/**/*",
      "node_modules/cors/**/*",
      "node_modules/dotenv/**/*",
      "node_modules/event-dispatch/**/*",
      "node_modules/express/**/*",
      "node_modules/express-basic-auth/**/*",
      "node_modules/faker/**/*",
      "node_modules/figlet/**/*",
      "node_modules/formidable/**/*",
      "node_modules/glob/**/*",
      "node_modules/helmet/**/*",
      "node_modules/hummus-recipe/**/*",
      "node_modules/jsonfile/**/*",
      "node_modules/jsonwebtoken/**/*",
      "node_modules/lodash/**/*",
      "node_modules/microframework-w3tec/**/*",
      "node_modules/moment/**/*",
      "node_modules/morgan/**/*",
      "node_modules/multer/**/*",
      "node_modules/mysql/**/*",
      "node_modules/node-cron/**/*",
      "node_modules/node-fetch/**/*",
      "node_modules/nps/**/*",
      "node_modules/nps-utils/**/*",
      "node_modules/pdffiller/**/*",
      "node_modules/php-serialize/**/*",
      "node_modules/reflect-metadata/**/*",
      "node_modules/routing-controllers/**/*",
      "node_modules/serve-favicon/**/*",
      "node_modules/ssh2-sftp-client/**/*",
      "node_modules/supertest/**/*",
      "node_modules/ts-node/**/*",
      "node_modules/tslint/**/*",
      "node_modules/typedi/**/*",
      "node_modules/typeorm/**/*",
      "node_modules/typeorm-seeding/**/*",
      "node_modules/typeorm-typedi-extensions/**/*",
      "node_modules/typescript/**/*",
      "node_modules/uuid/**/*",
      "node_modules/winston/**/*"
    ]
  }

What you expected to happen:
When I do with https://github.com/w3tecch/express-typescript-boilerplate and do : pkg .

It create files "sl-server-macos", "sl-server-linux". But when I do : ./sl-server-macos

And have an error :
error: [:snapshot/sl-server-pkgapp] Application is crashed: SyntaxError: Cannot use import statement outside a module

Thanks for help

@toothbrush7777777
Copy link

toothbrush7777777 commented May 15, 2020

I have the same issue. I have "type": "module" in my package.json.

@naliferov
Copy link

same

@hipstersmoothie
Copy link
Contributor

duplicate of #782

@azerpas
Copy link

azerpas commented Aug 5, 2021

For those using Typescript, set

    "target": "ES2020",                               
    "module": "commonjs",

inside your tsconfig.json

@unphased
Copy link

unphased commented Sep 5, 2023

i got pretty far with this, and it looks like the fact that I actually have a ESM project with "type": "module" in the package.json is causing pkg to pick this up and emit senseless esm code which fails to run, even though I've been able to babel-compile my source into cjs code inside my dist/ which is then sent to pkg.

I'm going to try to give pkg a fake package.json now and see if that works.

Update: I tried this and it did not fix the behavior. Still producing SyntaxError: Cannot use import statement outside a module.

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

No branches or pull requests

6 participants