代码上传到托管后为什么不起作用?它在本地服务器上正常运行。

huangapple go评论51阅读模式
英文:

Why did the code not work after uploading the file to the hosting? It works on the local server

问题

Code在将文件上传到托管服务器后为什么不起作用?在我的本地环境中是有效的。

以下是我的代码:

const express = require('express');

const app = express();
app.set('view engine', 'ejs');

app.use(express.static('/public'));

app.get('', (req, res) => {
    res.render('index', {layout: 'layout'});
});

app.listen();

出现了以下错误:

SyntaxError: Cannot use import statement outside a module
 Module.load (node:internal/modules/cjs/loader:1004:32)
/home/apka/apka/app.js:1
import express from 'express';

使用以下代码可以工作:

const express = require('express');

但以下代码不起作用:

import express from 'express';
英文:

Why the code is not working after uploading the file to the hosting server?
It works on my local environment

here is my code:

import express from 'express';

const app = express();
app.set('view engine', 'ejs');

app.use(express.static('/public'));

app.get('', (req, res) => {
    res.render('index', {layout: 'layout'});
});

app.listen()

Got the following error:

SyntaxError: Cannot use import statement outside a module
at Module.load (node:internal/modules/cjs/loader:1004:32)
/home/apka/apka/app.js:1
import express from 'express';

It worked with
const express = require('express')

but it doesn't work with:
import express from 'express';

答案1

得分: 1

你应该检查你的本地机器是否与你的开发环境完全相同。
特别要注意 NodeJS 的版本。

在所有 JavaScript/NodeJS 版本中,import 语句并不可用。

英文:

You should check that your local machine is identical to your development environment.
Have a look specifically on NodeJS version.

import statement is not available in all JavaScript/NodeJS versions

huangapple
  • 本文由 发表于 2023年5月22日 22:17:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76307127.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定