Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.7k views
in Technique[技术] by (71.8m points)

node.js - "Uncaught (in promise) ReferenceError: process is not defined" when migrating to Parcel 2

I'm trying to migrate a Node.js webapp from Parcel 1 to Parcel 2.

I have a function in the client-side javascript code (that Parcel bundles) that calls another function I'm importing from a utility functions file in the back-end Node.js code.

All other front-end functions work and all other Node.js functions which require Node.js process still work. When I trigger calling this function in the code:

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

Everything worked just fine in Parcel 1, so I'm assuming this is a problem with my Parcel 2 configuration, not with Cloudinary.

The offending lines:

In getColudinaryUrl.js (back-end):

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,
});

In index.js (front-end):

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

In server.js (back-end) This is the only place in the code where I do dotenv.config:

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

My OLD package.json with Parcel 1 which worked:

...
"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"
  }

My NEW package.json file which doesn't work:

...
"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
    }

  }

I added rm -rf .parcel-cache/ && since otherwise a second build would always fail.

I read the migration guide and several other pages:

It wasn't easy for me to read and, being rather new, Parcel 2 doesn't have many resources online to read over. That's how I ended up with the new package.json file above which gave me the least amount of errors (excluding the one above).

If there's anything else I should add to the question, I will gladly provide it.

How do I configure Parcel 2 to detect process in that one file?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...