我无法运行文件服务器。 JS。我已经尝试在 package.Json 中添加 “type”: module 并没有奏效。第一个代码是 server.js,第二个是 package.json。我也认为我不能使用 import 和 require?我不确定。
const express = require('express');
const app = express();
import { getUserList } from './user';
const userList = getUserList();
app.get('/users', (req, res, next) => {
return res.status(200).send({
success: 'true',
message: 'users',
users: userList
});
});
app.listen(8000, () => {
console.log('Example app listening on port 8000!');
});
{
"name": "learningnodejs",
"version": "1.0.0",
"description": "\"\",",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon server.js --exec babel-node --trace-warnings ...",
"lint": "eslint src/js"
},
"author": "",
"license": "ISC",
"dependencies": {
"@types/node": "^15.3.0",
"body-parser": "^1.19.0",
"express": "^4.17.1",
"nodemon": "^2.0.7"
},
"devDependencies": {
"@babel/cli": "^7.14.3",
"@babel/core": "^7.14.3",
"@babel/preset-env": "^7.14.2",
"babel-cli": "^6.26.0",
"babel-loader": "^8.2.2",
"babel-preset-env": "^1.7.0",
"webpack": "^5.37.0"
}
}