1
votes

I have made my first own project and i try to build my app and launch it after compilation. When I am using my app in dev mode with ts-node there is no problem, no error everything is okay. Now I compile with the build script: npm run start (check package.json below) and after my app is build in the dist folder I try to launch it and i see this errors:

Cannot use import statement outside a module Unexpected token 'export' file: ./src/modules/authentication/events/authentication.event.ts Cannot use import statement outside a module file: ./src/modules/users/events/user.event.ts

I have check on the web, but I have seen many response: add type:module in package.json, change typescript options, ... but nothing seems to work and I am a little lost among the different response I have seen.

I let you my config file to see if there is a problem:

Thx by advance for helping

Best regards

package.json
{
"name": "stygma-api",
"version": "1.0.0",
"description": "",
"main": "index.js",
"types": "dist/index.d.ts",
"keywords": [],
"author": "Seyrinian",
"license": "MIT",
"scripts": {
    "dev": "cross-env NODE_ENV=development nodemon --exec ts-node ./src/index.ts",
    "lint": "eslint . --fix",
    "start": "tsc && node dist/index.js",
    "test": "cross-env NODE_ENV=test jest --verbose --forceExit --coverage --runInBand",
    "test-watch": "cross-env NODE_ENV=test jest --verbose --watchAll --runInBand",
    "ts-check": "tsc --noEmit --pretty"
},
"dependencies": {
    "bcrypt": "^5.0.0",
    "dotenv": "^8.2.0",
    "eventemitter2": "^6.4.3",
    "express": "^4.17.1",
    "glob": "^7.1.6",
    "jsonwebtoken": "^8.5.1",
    "lodash": "^4.17.20",
    "moment": "^2.28.0",
    "mongoose": "^5.10.2",
    "mongoose-id-validator": "^0.6.0",
    "mongoose-unique-validator": "^2.0.3",
    "supertest": "^5.0.0",
    "swagger-jsdoc": "^5.0.1",
    "swagger-ui-express": "^4.1.4",
    "winston": "^3.3.3"
},
"devDependencies": {
    "@types/bcrypt": "^3.0.0",
    "@types/express": "^4.17.8",
    "@types/glob": "^7.1.3",
    "@types/jest": "^26.0.12",
    "@types/jsonwebtoken": "^8.5.0",
    "@types/lodash": "^4.14.165",
    "@types/mongoose": "^5.7.36",
    "@types/mongoose-id-validator": "^0.6.0",
    "@types/mongoose-unique-validator": "^1.0.4",
    "@types/node": "^14.6.2",
    "@types/supertest": "^2.0.10",
    "@types/swagger-jsdoc": "^3.0.2",
    "@types/swagger-ui-express": "^4.1.2",
    "@typescript-eslint/eslint-plugin": "^4.1.1",
    "@typescript-eslint/parser": "^4.1.1",
    "cross-env": "^7.0.2",
    "eslint": "^7.9.0",
    "eslint-config-airbnb-base": "^14.2.0",
    "eslint-config-prettier": "^6.11.0",
    "eslint-plugin-import": "^2.22.0",
    "eslint-plugin-prettier": "^3.1.4",
    "jest": "^26.4.2",
    "nodemon": "^2.0.4",
    "prettier": "^2.1.1",
    "ts-jest": "^26.3.0",
    "ts-node": "^9.0.0",
    "typescript": "^4.0.2"
}

}

tsconfig.json
{
"include": ["src/**/*"],
"exclude": ["node_modules", ".vscode", "dist"],
"compilerOptions": {
    "target": "es6" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */,
    "module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */,
    "declaration": true /* Generates corresponding '.d.ts' file. */,
    "outDir": "dist" /* Redirect output structure to the directory. */,
    "rootDir": "src" /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */,
    "strict": true /* Enable all strict type-checking options. */,
    "noUnusedLocals": true /* Report errors on unused locals. */,
    "noUnusedParameters": true /* Report errors on unused parameters. */,
    "noImplicitReturns": true /* Report error when not all code paths in function return a value. */,
    "noFallthroughCasesInSwitch": true /* Report errors for fallthrough cases in switch statement. */,
    "typeRoots": ["./src/typings", "./node_modules/@types"] /* List of folders to include type definitions from. */,

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

}

1

1 Answers

0
votes

Setting the type to module inside the package js normally fixes this problem. Are you sure you have set it right?

Please try to set it once again by adding it to the top layer of your package.json like this:

{
  ...
  "main": "index.js",
  "type": "module",
  ...
}

The "main" part here is just for clarification on where the "type" property has to go.