编译出错不能在模块外使用导入语句

时间:2021-07-08 23:33:22

标签: node.js typescript import error-handling build

我制作了自己的第一个项目,并尝试构建我的应用程序并在编译后启动它。当我在 ts-node 的开发模式下使用我的应用程序时,没有问题,没有错误,一切正常。现在我使用构建脚本进行编译:npm run start(检查下面的 package.json),在我的应用程序在 dist 文件夹中构建后,我尝试启动它,我看到以下错误:

不能在模块外使用 import 语句 意外的令牌“导出”文件:./src/modules/authentication/events/authentication.event.ts 不能在模块文件外使用 import 语句:./src/modules/users/events/user.event.ts

我在网上查过,但我看到很多回复:在 package.json 中添加 type:module,更改打字稿选项,...但似乎没有任何效果,我在不同的回复中有点迷失看到了。

我让你看看我的配置文件有没有问题:

感谢您的帮助

最好的问候

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 个答案:

答案 0 :(得分:0)

在包 js 中将类型设置为模块通常可以解决此问题。 你确定你设置对了吗?

请尝试再次设置它,将其添加到 package.json 的顶层,如下所示:

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

这里的“主要”部分只是为了澄清“类型”属性的去向。