【问题标题】:vercel import work in some file and not in another file, nextjs nodejsvercel 导入在某个文件中工作,而不是在另一个文件中,nextjs nodejs
【发布时间】:2021-10-14 16:36:06
【问题描述】:

错误发生日志:

13:24:54:83
2021-08-11T07:54:55.292Z    undefined   ERROR   (node:8) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
2021-08-11T07:54:55.303Z    undefined   ERROR   /var/task/api/userLoginEmail.js:8
export default async function (req, res) {
^^^^^^
SyntaxError: Unexpected token 'export'
    at wrapSafe (internal/modules/cjs/loader.js:1001:16)
    at Module._compile (internal/modules/cjs/loader.js:1049:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
    at Module.load (internal/modules/cjs/loader.js:950:32)
    at Function.Module._load (internal/modules/cjs/loader.js:790:14)
    at ModuleWrap.<anonymous> (internal/modules/esm/translators.js:199:29)
    at ModuleJob.run (internal/modules/esm/module_job.js:169:25)
    at async Loader.import (internal/modules/esm/loader.js:177:24)
    at async getListener (/var/task/___vc/__launcher.js:33:11)
   RequestId: df65f579-7eb4-4d5b-8f43-ed01c4e5ecd1 Error: Runtime exited with error: exit status 1
   Runtime.ExitError

即使在将 type: module 添加到 package json 之后,它也会因其他文件而中断,但类似的代码会通过而不记录任何日志?怎么可能只是为了这个?

这是其他文件的起始代码,它可以在没有任何错误日志的情况下工作


   import Cookies from "cookies";
   import { createEmailTemplate } from "./functions";

   export default async function (req, res) {
    // Create a cookies instance
    const cookies = new Cookies(req, res);

    // // Get a cookie
    // LINE BELOW COMMENTED FOR VERCEL DEPLOYMENT TEST!
    if (req.method !== "POST" && cookies.get("kAppToken") !== null && 
    process.env.API_ACCESS_SECRET_TOKEN) return res.status(403).send(res.send("not allowed"));```

【问题讨论】:

    标签: node.js import export package.json vercel


    【解决方案1】:

    给你的函数起个名字怎么样?

    export default async function handler(req, res) {}
    

    【讨论】:

    • 其他文件也没有指定任何名称并且它可以工作,这种代码用于nextjs应用程序中的无服务器功能。我想这不是错误的原因,我弄错了吗?
    • 但是你有没有尝试命名它?
    • 不,命名没有区别。我在本地调试文件后发现,代码中存在拼写错误导致 vercel 失败。之后上面没有错误。
    猜你喜欢
    • 1970-01-01
    • 2020-06-29
    • 1970-01-01
    • 1970-01-01
    • 2014-09-20
    • 2020-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多