无法在模块外使用导入语句 [英] Cannot use import statement outside modules

查看:88
本文介绍了无法在模块外使用导入语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将NextJS与打字稿,mongo Atlas,mongoose,node和express一起使用.

I am using NextJS with typescript, mongo Atlas, mongoose, node and express.

运行节点页面/服务器时出现以下错误:我已经上传了package.json文件,并且还添加了babel

I am getting the following error when I run node pages/server: I have uploaded my package.json file and have added babel as well

从'express'进口快递;^^^^^^

import express from 'express'; ^^^^^^

SyntaxError:无法在模块外部使用import语句在wrapSafe(内部/模块/cjs/loader.js:1072:16)在Module._compile(internal/modules/cjs/loader.js:1122:27)在Object.Module._extensions..js(内部/模块/cjs/loader.js:1178:10)在Module.load(internal/modules/cjs/loader.js:1002:32)在Function.Module._load(internal/modules/cjs/loader.js:901:14)在Function.executeUserEntryPoint [作为runMain](内部/模块/run_main.js:74:12)在internal/main/run_main_module.js:18:47

SyntaxError: Cannot use import statement outside a module at wrapSafe (internal/modules/cjs/loader.js:1072:16) at Module._compile (internal/modules/cjs/loader.js:1122:27) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10) at Module.load (internal/modules/cjs/loader.js:1002:32) at Function.Module._load (internal/modules/cjs/loader.js:901:14) at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12) at internal/main/run_main_module.js:18:47

这是我的server.js代码:

This is my server.js code:

import express from 'express';
import { connect, connection } from 'mongoose';
import morgan from 'morgan';
import path from 'path';
const app = express();
const PORT = process.env.PORT || 8080;
//Success
   import routes from './routes/api.tsx';

const MONGODB_URI = 'xxx';

// const routes=require('./routes/api')
connect(MONGODB_URI ||'mongodb://localhost/success', {
    useNewUrlParser: true,
    useUnifiedTopology: true
});

connection.on('connected', () => {
    console.log('Mongoose is connected');
});

const newBlogPost = new BlogPost(data); //instance of the model

app.use(morgan('tiny'));
app.use('/',routes)

app.listen(PORT, console.log(`Server is starting at ${PORT}`));

package.json文件

{
  "name": "la-sheild",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "dev": "next",
    "build": "next build",
    "start": "babel-node server.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@types/express": "^4.17.2",
    "@types/mongoose": "^5.7.1",
    "axios": "^0.19.2",
    "concurrently": "^5.1.0",
    "express": "^4.17.1",
    "mongoose": "^5.9.1",
    "morgan": "^1.9.1",
    "next": "^9.2.2",
    "node": "^13.8.0",
    "react": "^16.12.0",
    "react-dom": "^16.12.0"
  },
  "devDependencies": {
    "@babel/core": "^7.8.4",
    "@babel/preset-env": "^7.8.4",
    "@babel/register": "^7.8.3",
    "@types/node": "^13.7.4",
    "@types/react": "^16.9.21",
    "babel-cli": "^6.26.0",
    "typescript": "^3.7.5"
  },
  "proxy": "http://localhost:8080"
}

推荐答案

自Node v12起,您可以使用 .mjs 扩展名或设置"type":"module" package.json 中的code>.

Since Node v12, you can use either the .mjs extension or set "type": "module" in your package.json.

并且您需要使用- -experimental-modules 标志运行节点.

And you need to run node with the --experimental-modules flag.

node --experimental-modules server.mjs

您可以检查 SO链接

或者您可以在项目的根目录中创建 .babelrc 文件.添加以下内容(以及您需要的任何其他babel预设,都可以在此文件中添加):

Or you can create .babelrc file in the root of your project. Add following (and any other babel presets you need, can be added in this file):

{
    "presets": ["env"]
}

使用

npm install babel-preset-env
npm install babel-cli -g

# OR

yarn add babel-preset-env
yarn global add babel-cli

现在,转到 server.js 文件所在的文件夹,然后

Now, go to the folder where your server.js file exists and

使用以下命令运行:

babel-node fileName.js

或者您可以通过将以下代码添加到 package.json 文件中来使用npm start运行:

Or you can run using npm start by adding following code to your package.json file:

"scripts": {
    "start": "babel-node server.js"
}

有一个教程链接,用于使用自定义Express Server + Typescript设置Next.js,这对您非常有帮助.

There is a tutorial link for Set Up Next.js with a Custom Express Server + Typescript on a medium that will be very helpful for you.

这篇关于无法在模块外使用导入语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆