英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论