简体   繁体   English

Nodejs运行js报错:Cannot use import statement outside a module

[英]Nodejs run js error: Cannot use import statement outside a module

I can't run node src/index.js it throws errors.我无法运行 node src/index.js 它会抛出错误。 But nodemon is ok.但是nodemon没问题。 How do i run a production version of it?我如何运行它的生产版本?

PS D:\Development\server\Image hosting\node-express-server-rest-api-master> node   src/index.js
D:\Development\server\Image hosting\node-express-server-rest-api-master\src\index.js:2
import cors from 'cors';

SyntaxError: Cannot use import statement outside a module

It's a Very simple project and the files: I tried the "type": "module", But it throws new error.这是一个非常简单的项目和文件:我尝试了“type”:“module”,但它抛出了新的错误。 Nothing works dispite this is just a example project尽管这只是一个示例项目,但没有任何效果

package.json : package.json :

{
"name": "node-express-server-rest-api",
"version": "0.1.0",
"description": "Node imgs hosting",
"scripts": {
    "start": "nodemon --exec babel-node src/index.js",
    "test": "echo \"No test specified\" && exit 0",
    "production": "NODE_ENV=production node src/index.js",
    "win-prod": "set NODE_ENV=production && node   src/index.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
    "@babel/core": "^7.11.6",
    "@babel/node": "^7.0.0",
    "@babel/preset-env": "^7.1.6",
    "@babel/register": "^7.0.0"
},
"dependencies": {
    "body-parser": "^1.19.0",
    "cors": "^2.8.5",
    "dotenv": "^8.2.0",
    "express": "^4.17.1",
    "follow-redirects": "^1.13.0",
    "mkdirp": "^1.0.4",
    "uuid": "^7.0.3"
  }
 }

index.js索引.js

require('dotenv/config');
import cors from 'cors';
import express from 'express';


import routes from './routes';
const bodyParser = require("body-parser");

const app = express();


app.use(cors());

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());


app.use((req, res, next) => {
// req.context = {
//     models,
//     me: models.users[1],
// };
next();
});


app.use('/upload', routes.upload);


app.listen(process.env.PORT, () =>
 console.log(`Example app listening on port ${process.env.PORT}!`),
);

I'll Post the answer here.我会在这里发布答案。

Add this module: npm i esm添加此模块: npm i esm

Add one more file under the root folder I'll name it as main.js:在根文件夹下再添加一个文件,我将其命名为 main.js:

require = require("esm")(module /*, options*/ )
module.exports = require("./src/index.js")

Change the package.json start script From this:更改 package.json 启动脚本从此:

 "win-prod": "set NODE_ENV=production && node -r src/index.js"

into this:进入这个:

 "win-prod": "set NODE_ENV=production && node -r esm main.js"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 NodeJS 错误“不能在模块外使用导入语句” - NodeJS error "cannot use import statement outside a module" NodeJS --> SyntaxError: 不能在模块外使用 import 语句 - NodeJS --> SyntaxError: Cannot use import statement outside a module JS 导入导出语法错误:不能在模块外使用导入语句 - JS import export SyntaxError: Cannot use import statement outside a module 使用 Jest 测试 Nodejs 项目时出现“SyntaxError: Cannot use import statement outside a module”错误 - "SyntaxError: Cannot use import statement outside a module" error while testing a Nodejs project with Jest WordPress JavaScript 导入错误 不能在模块外使用导入语句 - WordPress JavaScript import error Cannot use import statement outside a module Javascript 模块错误语法错误:不能在模块外使用导入语句 - Javascript Module error SyntaxError: Cannot use import statement outside a module Node.js:语法错误:无法在模块外使用导入语句 - Node.js: SyntaxError: Cannot use import statement outside a module JS npm 包,不能在模块外使用 import 语句 - JS npm package, Cannot use import statement outside a module 未捕获的语法错误:无法在节点 js 中的模块外使用导入语句 - Uncaught SyntaxError: Cannot use import statement outside a module in node js 语法错误:不能在模块 discord.js 之外使用导入语句 - SyntaxError: Cannot use import statement outside a module discord.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM