简体   繁体   English

使用 TypeScript 和 nodemon: SyntaxError: Cannot use import statement outside a module

[英]Using TypeScript and nodemon: SyntaxError: Cannot use import statement outside a module

I am converting the code to use nodemon to leverage TypeScript.我正在转换代码以使用 nodemon 来利用 TypeScript。

In my package.json :在我的package.json

  "scripts": {
    "serve-fake-api": "nodemon fake-api/server.ts --watch 'fake-api/*.*'  ",
    "serve-vue": "vue-cli-service serve",
    "serve": "concurrently -k \"npm run serve-fake-api\" \"npm run serve-vue\"",
    "build": "vue-cli-service build",
    "lint": "vue-cli-service lint"
  },

and the fake-api/server.ts file:fake-api/server.ts文件:

import { readFileSync } from 'fs';
import { create, defaults, bodyParser, rewriter, router as _router } from 'json-server';
import { join } from 'path';

const server = create();
const defaultMiddleware = defaults();

// It is recommended to use the bodyParser middleware before any other middleware in your application
server.use(bodyParser);

server.use(defaultMiddleware);

// Define custom routes (routes.json)
const routes = JSON.parse(readFileSync(join(__dirname, 'routes.json'), "utf8"));
server.use(rewriter(routes));

// Add custom middleware before JSON Server router
const customMiddleware = require(join(__dirname, 'middleware.ts'));
server.use(customMiddleware);

// This is where `json-server`'s magic happens ;)
const router = _router(join(__dirname, 'db.json'));

// Start the application by listening to port 3000,
// Although this won't print the nice starting message you see when
// running `json-server` as CLI command, it still runs the app correctly.
server.use(router);
server.listen(3000, () => {
  console.log('JSON Server is running')
});

but when running npm run serve :但是在运行npm run serve

[0] C:\Users\eperret\Desktop\tabulator-tests\fake-api\server.ts:1
[0] import { readFileSync } from 'fs';
[0] ^^^^^^
[0]
[0] SyntaxError: Cannot use import statement outside a module

I googled a bit and ended up here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import我用谷歌搜索了一下,最后在这里: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import

Is there a workaround to keep using this kind of import ?有没有办法继续使用这种import

I answered to my question on the related GitHub issue thread: https://github.com/remy/nodemon/issues/1625#issuecomment-560115741我在相关的 GitHub 问题线程上回答了我的问题: https://github.com/remy/nodemon/issues/1625#issuecomment-560115741

I solved my issue by changing the module type with commonjs in tsconfig.json instead of esnext :我通过在tsconfig.json而不是esnext中使用commonjs更改模块类型解决了我的问题:

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SyntaxError:无法在模块 webpack-typescript 外使用 import 语句 - SyntaxError: Cannot use import statement outside a module webpack-typescript SyntaxError: 不能在模块外使用 import 语句。 我正在使用 typescript 和 discord js - SyntaxError: Cannot use import statement outside a module. I'm using typescript and discord js SyntaxError: 不能在模块外使用 import 语句 - SyntaxError: Cannot use import statement outside a module SyntaxError:无法使用 jest 在模块外使用 import 语句 - SyntaxError: Cannot use import statement outside a module using jest 无法在后端使用 typescript 的模块外部使用导入语句 - Cannot use import statement outside a module using typescript on backend 笑话:SyntaxError:无法在 React/Typescript 项目中的模块外使用 import 语句 - Jest: SyntaxError: Cannot use import statement outside a module in React/Typescript project 运行编译的 Javascript 代码时,TypeScript 文件中出现“语法错误:无法在模块外使用导入语句”错误 - "SyntaxError: Cannot use import statement outside of a module" error in a TypeScript file when running compiled Javascript code 带有 npm 模块的“语法错误:无法在模块外使用导入语句” - “SyntaxError: Cannot use import statement outside a module” with a npm module 为什么在使用 cloudinary 模块时出现“语法错误:无法在模块外使用导入语句”错误? - Why am I getting the 'SyntaxError: Cannot use import statement outside a module' error while using the cloudinary module? SyntaxError: 不能在模块外使用 import 语句是什么意思? - What does it mean SyntaxError: Cannot use import statement outside a module?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM