node.js - 错误 TS1259:模块 '"./node_modules/@types/express/index"' 只能使用 'esModuleInterop' 标志默认导入
问题描述
我正在尝试将 express 导入项目并且在开发过程中,它刚刚停止工作,我已经重置了配置等。无法使其工作,无法导入任何模块。
npx tsc src/server.ts
src/server.ts:1:8 - error TS1259: Module '"/home/fpc-ubut/Git/nlw2/server/node_modules/@types/express/index"' can only be default-imported using the 'esModuleInterop' flag
1 import express from 'express';
src/database/migrations/00_create_users.ts:1:8 - error TS1259: Module '"/home/fpc-ubut/Git/nlw2/server/node_modules/knex/types/index"' can only be default-imported using the 'esModuleInterop' flag
1 import Knex from 'knex'
src/server.ts:1:8 - error TS1259: Module '"path"' can only be default-imported using the 'esModuleInterop' flag
1 import path from 'path'
环境操作系统:Ubuntu 20.04.1 LTS
节点:v12.18.1
软件包:
"@types/express": "^4.17.6",
"@types/node": "^14.0.27",
"ts-node": "^8.10.2",
"ts-node-dev": "^1.0.0-pre.44",
"typescript": "^3.9.3"
"express": "^4.17.1",
"knex": "^0.21.1",
"pg": "^8.2.1"
EDIT1:如被问及,这是我的 tsconfig.json。但是配置(“esModuleInterop”:true)已经是方面
<tsconfig.json>
{
"compilerOptions": {
/* Visit https://aka.ms/tsconfig.json to read more about this file */
/* Basic Options */
// "incremental": true, /* Enable incremental compilation */
"target": "es5", /* 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'. */
// "lib": [], /* Specify library files to be included in the compilation. */
// "allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
// "declaration": true, /* Generates corresponding '.d.ts' file. */
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
// "sourceMap": true, /* Generates corresponding '.map' file. */
// "outFile": "./", /* Concatenate and emit output to single file. */
// "outDir": "./", /* Redirect output structure to the directory. */
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
// "composite": true, /* Enable project compilation */
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
// "removeComments": true, /* Do not emit comments to output. */
// "noEmit": true, /* Do not emit outputs. */
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
/* Strict Type-Checking Options */
"strict": true, /* Enable all strict type-checking options. */
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
// "strictNullChecks": true, /* Enable strict null checks. */
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
// "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
/* Additional Checks */
// "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. */
/* Module Resolution Options */
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
// "typeRoots": [], /* List of folders to include type definitions from. */
// "types": [], /* Type declaration files to be included in compilation. */
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
/* Source Map Options */
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
/* Experimental Options */
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
/* Advanced Options */
"skipLibCheck": true, /* Skip type checking of declaration files. */
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
}
}
编辑 2:更改 tsconfig.json
"allowSyntheticDefaultImports": true
仍然显示相同的错误
编辑 3:Github 链接:[删除]
问题是./server
./server2
工作得很好。相同的配置。
EDIT4:已解决
根据您的 GitHub 存储库,./server 没有 tsconfig 文件,而 .server2 确实有一个。如果我需要更清楚一点:它们没有相同的配置,因为服务器甚至没有配置。您应该将 ./server/src/tsconfig.json 移动到 ./server/tsconfig.json,就像您的 tsconfig 文件在 ./server2 中一样(注意我说的是 MOVE not COPY the tsconfig).. – Matt Oestreich
解决方案
就我而言,我正面临这个反应错误。我通过更改我的导入样式来修复它
import React from 'react';
至
import * as React from 'react';
推荐阅读
- azure - 如何通过 Visual Studio 2019 在 Azure Functions 中渲染 Razor 页面?
- python - 在多部分 html 电子邮件中嵌入图像 - 正确定义链接的图像
- c# - Asp.Net Core 3.1 数据被检索但未保存到数据库中
- android - react-native 如何使用链接打开本地文件 url?
- jquery - 如何从动态填充的表格中获取可点击单元格的数据
- amazon-web-services - 当消息被添加到 SQS 时,仅向 lambda 发送事件主体而不是整个事件
- ios - 我如何知道 NFC 护照芯片读取了哪个数据组?
- node.js - npm package.json 别名,例如 webpack
- python - 理解将月份名称映射到其数值的字典
- c# - 如何从自定义基础项目创建 ASP.NET Core MVC 样板