错误TS1259:只能使用'esModuleInterop'标志默认导入模块'“ ./node_modules/@types/express/index”'

弗朗西斯科·佩纳

我正在尝试将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相同的配置。

编辑4:已解决

根据您的GitHub存储库,。/ server没有tsconfig文件,而.server2没有tsconfig文件。如果我需要更加清楚:它们没有相同的配置,因为服务器甚至没有配置。您应该将./server/src/tsconfig.json移至./server/tsconfig.json,就像您的tsconfig文件位于./server2中一样(注意,我说过不要复制tsconfig)。– – Matt Oestreich

马特·奥斯特赖希

根据您的GitHub存储库,您具有以下结构(某些文件/目录已被省略):

坏:

  • 服务器
    • src
      • tsconfig.json
  • 服务器2
    • src
    • tsconfig.json

你将需要移动/server/src/tsconfig.json/server/tsconfig.json-请注意,您将需要移动,而不是复制tsconfig文件。

好:

  • 服务器
    • src
    • tsconfig.json
  • 服务器2
    • src
    • tsconfig.json

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

TypeScript:只能使用“esModuleInterop”标志默认导入

“只能使用'esModuleInterop'标志默认导入”是什么意思?

有没有办法在tsconfig中使用--esModuleInterop而不是将其作为标志?

使用babel从node_modules导入模块但失败

模块为 CommonJS 时 ts-node 中的“SyntaxError:无法在模块外使用导入语句”错误

导入映像时,TS无法从node_modules中找到模块

了解tsconfig文件中的esModuleInterop

使用模块时如何避免导入默认符号?

TypeScript:从node_modules导入外部模块

Typescript文件中的导入模块给出错误“除非提供'--module'标志,否则无法编译模块”

Node.js 错误 - '不能在模块外使用导入'

使用前缀从 node_modules 导入 Vue

设置了--experimental-modules的“无法在模块外部使用导入语句”

TypeScript 1.5:CommonJS'export ='的ES6模块默认导入(仅.d.ts问题?)

为什么我不能使用SystemJS直接从node_modules导入?

从 Typescript 生成的导入 Node.js 模块的“默认”部分是什么?

Angular 8-延迟加载模块:错误TS1323:仅当'--module'标志为'commonjs'或'esNext'时才支持动态导入

从外部模块“汇总”中导入了“默认”,但从未使用过

Babelify在从node_modules导入模块时抛出ParseError

Node.js:SyntaxError:无法在模块外部使用导入语句

使用“导入模块”还是“从模块导入”?

导入模块错误

该模块导入错误

Xcode模块导入错误

Angular 导入模块错误

skbio 模块的导入错误

如何在Node中导入全局模块?我收到“错误:找不到模块<module>”?

使用TypeScript的默认导入

ES6使用--experimental-modules导入Node