SyntaxError,即使在package.json中使用“ type”:“ module”,也无法在模块外部使用import语句

问题描述

我正在尝试使用ES6模块导入Express。尽管我在package.json中添加"type":"module"错误SyntaxError: Cannot use import statement outside a module仍然会发生。我希望得到一个不需要转换为.ejs扩展名的答案,因为它想知道我的代码中有什么问题而不是采取替代方法。注意:package.jsonserver.js在同一目录中。

package.json

{
  "name": "","version": "1.0.0","description": "","main": "server.js","type": "module","scripts": {
    "test": "echo \"Error: no test specified\" && exit 1","start": "node server.js"
  },"author": "","license": "ISC","dependencies": {
    "express": "^4.17.1","mongoose": "^5.10.3","nodemon": "^2.0.4"
  }
}

Server.js

import express from 'express';
const app = express()
const port = process.env.PORT || 9000

app.get('/',(req,res) => {
    res.status(200).send('hello world');
})

app.listen(port,() => {
    console.log(`Listening on localhost: ${port}`)
});

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)