如何解决在Node.js中导入模块时出现的”Unexpected token, expected”错误。

huangapple go评论67阅读模式
英文:

How to resolve Unexpected token, expected during import module in nodejs

问题

I don't see where is my error in this code, can you help me?
我不知道我的代码哪里出错了,你能帮我吗?
import app from "../app";
从"../app"导入app;

I had add this "type": "module" in my package.json but my server stop to run.
我已经在我的package.json中添加了"type": "module",但我的服务器停止运行了。

const request = require("supertest");
import app from "../app";

describe("Test the root path", () => {
test("It should response the GET method", () => {
return request(app).get('/').then((res) => {
expect(res.statusCode).toBe(200);
});
});
});

英文:

I don't see where is my error in this code, can you help me?
import app from "../app";
^^^^^^

I had add this "type": "module" in my package.json but my server stop to run.

const request = require("supertest");
import app from "../app";



describe("Test the root path", () => {
  test("It should response the GET method", () => {
    return request(app).get('/').then((res) => {
        expect(res.statusCode).toBe(200);
      });
  });
});

答案1

得分: 2

你不能将" type": module 设置与使用 require 的 CommonJs 一起使用 ES6 模块,所以请选择其中一个你喜欢的:

CommonJs:

const request = require("supertest");
const app = require("../app.js");

ES 模块:

import request from 'supertest'; // 现在 supertest 应该支持 ES6。
import app from "../app.js";
英文:

you can't use ES6 module with "type": module setting together with CommonJs which use require, so pick one you prefer :

CommonJs :

const request = require("supertest");
const app = require("../app.js");

ES module:

import request from 'supertest'; // supertest should now support ES6.
import app from "../app.js";

huangapple
  • 本文由 发表于 2023年1月6日 05:48:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75024700.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定