【发布时间】: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/ &&,否则第二次构建总是会失败。
我阅读了迁移指南和其他几页:
- https://v2.parceljs.org/getting-started/migration/
- https://v2.parceljs.org/features/module-resolution/
- https://v2.parceljs.org/features/node-emulation/
对我来说阅读起来并不容易,而且由于相当新,Parcel 2 没有太多在线资源可供阅读。这就是我最终得到上面新的 package.json 文件的方式,它给了我最少的错误(不包括上面的那个)。
如果还有什么需要补充的,我很乐意提供。
如何配置 Parcel 2 以检测该文件中的进程?
【问题讨论】:
标签: node.js cloudinary parceljs