英文:
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";
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论