Javascript ReferenceError:未定义模块

Javascript ReferenceError:未定义模块,javascript,node.js,reactjs,syntax-error,node-modules,Javascript,Node.js,Reactjs,Syntax Error,Node Modules,所以我一直在尝试运行这个web应用程序,最初它显示了 (节点:12960)警告:要加载ES模块,请在package.json中设置“type”:“module”或使用.mjs扩展名。 C:\Users\J\react messenger\stream聊天模板api\src\index.js:1 从“dotenv”导入dotenv; ^^^^^^ SyntaxError:无法在模块外部使用导入语句 然后我在package.json中设置type:module,但它给了我这个错误 Reference

所以我一直在尝试运行这个web应用程序,最初它显示了

(节点:12960)警告:要加载ES模块,请在package.json中设置“type”:“module”或使用.mjs扩展名。
C:\Users\J\react messenger\stream聊天模板api\src\index.js:1
从“dotenv”导入dotenv; ^^^^^^

SyntaxError:无法在模块外部使用导入语句

然后我在package.json中设置type:module,但它给了我这个错误

ReferenceError:未定义模块

at file:///C:/Users/J/react-messenger/stream-chat-boilerplate-api/src/index.js:38:1
这是我的密码:

import dotenv from 'dotenv';
dotenv.config();

import fs from 'fs';
import path from 'path';
import express from 'express';
import bodyParser from 'body-parser';
import cors from 'cors';
import helmet from 'helmet';
import compression from 'compression';

const api = express();

api.use(cors());
api.use(compression());
api.use(helmet());
api.use(bodyParser.urlencoded({ extended: true }));
api.use(bodyParser.json());

api.listen(process.env.PORT, error => {
    if (error) {
        console.warn(error);
        process.exit(1);
    }

    // eslint-disable-next-line array-callback-return
    fs.readdirSync(path.join(__dirname, 'routes')).map(file => {
        require('./routes/' + file)(api);
    });

    console.info(
        `Running on port ${process.env.PORT} in ${
            process.env.NODE_ENV
        } mode. You are mixing ES imports with CommonJS - at bottom of file you have 
module.exports = api;
which is CJS terminology. The ES module equivalent is:

export default 
从“dotenv”导入dotenv;
dotenv.config();
从“fs”导入fs;
从“路径”导入路径;
从“快递”进口快递;
从“body parser”导入bodyParser;
从“cors”进口cors;
从“头盔”导入头盔;
从“压缩”导入压缩;
常量api=express();
api.use(cors());
api.use(compression());
api.使用(头盔());
use(bodyParser.urlencoded({extended:true}));
use(bodyParser.json());
侦听(process.env.PORT,错误=>{
如果(错误){
控制台。警告(错误);
过程。退出(1);
}
//eslint禁用下一行数组回调返回
fs.readdirSync(path.join(uu dirname,'routes')).map(文件=>{
需要('./路由/'+文件)(api);
});
console.info(
`在中的端口${process.env.port}上运行${
process.env.NODE_env

}模式。您将ES导入与CommonJS混合在一起-在文件底部有
module.exports=api;
,这是CJS术语。ES模块等效物为:


检查package.json上的dotenv,确保npm安装dotenv。

如其所示:在package.json中设置“type”:“module”或使用.mjs扩展名。正确,除了一个附加的
s
。它应该是:
导出默认值
Duh!Good catch@Holtwick