【问题标题】:"Uncaught (in promise) ReferenceError: process is not defined" when migrating to Parcel 2迁移到 Parcel 2 时出现“未捕获(承诺中)ReferenceError:未定义进程”
【发布时间】:2021-04-21 06:13:08
【问题描述】:

我正在尝试将 Node.js Web 应用从 Parcel 1 迁移到 Parcel 2。

我在客户端 javascript 代码(Parcel 捆绑)中有一个函数,它调用我从后端 Node.js 代码中的实用程序函数文件导入的另一个函数。

所有其他前端功能都可以工作,并且所有其他需要 Node.js 进程的 Node.js 功能仍然可以工作。 当我在代码中触发调用这个函数时:

getCloudinaryUrl.js:22 Uncaught (in promise) ReferenceError: process is not defined

在 Parcel 1 中一切正常,所以我假设这是我的 Parcel 2 配置的问题,而不是 Cloudinary。

违规行:

在 getColudinaryUrl.js(后端)中:

const { Cloudinary } = require('cloudinary-core');
...
// this is what triggers the error
const cloudName = process.env.CLOUDINARY_CLOUD_NAME;
const cl = new Cloudinary({
  cloud_name: cloudName,
});

在 index.js(前端)中:

import getCloudinaryUrl from './../../utils/getCloudinaryUrl';
// then I'm calling it later on in the code

在 server.js 中(后端) 这是代码中我做 dotenv.config 的唯一地方:

const dotenv = require('dotenv');
...
dotenv.config({ path: './.env' });

我的旧 package.json 与 Parcel 1 一起工作:

...
"scripts": {
    ...
    "watch:js": "parcel watch ./public/js/index.js --public-url /js --out-dir ./public/js --out-file bundle.js",
    "build:js": "parcel build ./public/js/index.js --public-url /js --out-dir ./public/js --out-file bundle.js"
  },
  "devDependencies": {
    ...
    "parcel-bundler": "1.12.3",
    ...
  },
  "engines": {
    "node": "^14"
  }

我的新 package.json 文件不起作用:

...
"scripts": {
    ...
    "watch:js": "rm -rf .parcel-cache/ && parcel watch ./public/js/index.js --public-url /js --dist-dir ./public/js",
    "build:js": "rm -rf .parcel-cache/ && parcel build ./public/js/index.js --public-url /js --dist-dir ./public/js"
  },
"devDependencies": {
    ...
    "parcel": "^2.0.0-nightly.524",
    ...
  },
"engines": {
    "node": "^14"
  },
"default": "./public/js/bundle.js",
"targets": { 
    "main": false,
    "default": {
      "includeNodeModules": true,

      "scopeHoist": false
    }

  }

我添加了rm -rf .parcel-cache/ &&,否则第二次构建总是会失败。

我阅读了迁移指南和其他几页:

对我来说阅读起来并不容易,而且由于相当新,Parcel 2 没有太多在线资源可供阅读。这就是我最终得到上面新的 package.json 文件的方式,它给了我最少的错误(不包括上面的那个)。

如果还有什么需要补充的,我很乐意提供。

如何配置 Parcel 2 以检测该文件中的进程?

【问题讨论】:

    标签: node.js cloudinary parceljs


    【解决方案1】:

    就像在项目根目录中将CLOUDINARY_CLOUD_NAME=something 添加到.env 文件中一样简单?

    【讨论】:

    • 我已经这样做了,因为它在使用 Parcel v1 之前就可以工作。不过,谢谢。有谁知道我可以在哪里获得更多帮助?
    • 这似乎是一个反问。如果您可以将答案表述得更加自信,那将会很有帮助。请访问how to answer
    猜你喜欢
    • 2014-06-05
    • 2020-02-29
    • 2022-06-15
    • 2021-11-08
    • 2018-01-31
    • 2019-01-16
    • 2017-05-12
    • 1970-01-01
    • 2021-04-26
    相关资源
    最近更新 更多