Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parcel cannot resolve Node.js lib/fs.js promise-based APIs from 'fs/promises' #7403

Closed
WesleyMConner opened this issue Dec 4, 2021 · 9 comments

Comments

@WesleyMConner
Copy link

WesleyMConner commented Dec 4, 2021

This issue is similar to Parcel issue #7329, which was closed by pull #7348.

🐛 bug report

When Parcel processes a trivial ES6 javascript file with an import from fs/promises, it complains that fs/promises is not declared in package.json.

🎛 Configuration

Node v16.13.0
Npm 8.1.0
No use of Babel or Frameworks

Complete Example

bug-report/
  |
  +-- lib/
  |     |
  |     +-- example.js   // Note: Same outcome w/ .mjs extension
  |
  +-- package.json

example.js

import {writeFile} from 'fs/promises';

const myFileWriter = (myFile) => {
  writeFile(myFile, 'This is a test');
};

export {myFileWriter};

package.json

{
  "name": "bug-report",
  "description": "Demo dependency built-in issue",
  "type": "module",
  "source": "lib/example.js",
  "main": "dist/index.js",
  "dependencies" :{
    "parcel": ">=2.0.1"
  },
  "scripts": {
    "build": "parcel build --log-level 'verbose' --detailed-report 100"
  }
}

🤔 Expected Behavior

Use of import {writeFile} from 'fs/promises'; should not require an external dependency entry in package.json.

😯 Current Behavior

Sample Run

$ npm install
   :

$ npm ls
bug-report@ /workspace/site-playbook/packages/bug-report
└── [email protected]

$ npm run build

> build
> parcel build --log-level 'verbose' --detailed-report 100

🚨 Build failed.

@parcel/core: Failed to resolve 'fs/promises' from 'bug-report/lib/example.js'

  /workspace/site-playbook/packages/bug-report/lib/example.js:1:25
  > 1 | import {writeFile} from 'fs/promises';
  >   |                         ^^^^^^^^^^^^^
    2 |
    3 | const myFileWriter = (myFile) => {

@parcel/resolver-default: External dependency "fs" is not declared in package.json.

  /workspace/site-playbook/packages/bug-report/package.json:7:3
    6 |   "main": "dist/index.js",
  > 7 |   "dependencies" :{
  >   |   ^^^^^^^^^^^^^^
    8 |     "parcel": ">=2.0.1"
    9 |   },

  💡 Add "fs" as a dependency.

💁 Possible Solution

Revisit and adjust Parcel pull #7348.

It is unclear whether this is a broad issue with built-in libraries or if the embedded character / in fs/promises contributes to the isseue.

🔦 Context

See the two-file example above.

💻 Code Sample

See package.json and example.js above.

🌍 Your Environment

Software Version(s)
Parcel 2.0.1
Node v16.13.0
npm 8.1.0
Distributor ID Ubuntu
Description Ubuntu 20.04.2 LTS
Release 20.04
Codename focal
@sepsol
Copy link

sepsol commented Dec 13, 2021

I'm having the same issue. Every time I try to build a file containing fs imports, build fails and it asks for me to add fs as a dependancy.

@mischnic
Copy link
Member

Revisit and adjust Parcel pull #7348.

You can test that PR with

  "dependencies": {
    "parcel": "2.0.0-nightly.945"
  },

which builds successfully. So this will be fixed in the next release

@sepsol
Copy link

sepsol commented Dec 15, 2021

@mischnic I tried it and this is what I'm getting:

$ npx parcel --version
2.0.0-nightly.945+68ceb558

$ npm run build

> [email protected] build
> lerna run build

lerna notice cli v4.0.0
lerna info Executing command in 3 packages: "npm run build"
lerna ERR! npm run build exited 1 in '@mbhc/forms-core'
lerna ERR! npm run build stdout:

> @mbhc/[email protected] prebuild
> rm -rf dist


> @mbhc/[email protected] build
> parcel build

Building...
lerna ERR! npm run build stderr:
DOMException [DataCloneError]: function fn(...args) {
    return new Promise((resolve, reject) => {
      original.call(this, ...args, (err, ....<omitted>... } could not be cloned.
    at Worker.postMessage (internal/worker.js:340:23)
    at ThreadsWorker.send (/path/to/monorepo/project/root/node_modules/parcel/node_modules/@parcel/core/node_modules/@parcel/workers/lib/threads/ThreadsWorker.js:74:17)
    at Worker.send (/path/to/monorepo/project/root/node_modules/parcel/node_modules/@parcel/core/node_modules/@parcel/workers/lib/Worker.js:145:17)
    at Worker.call (/path/to/monorepo/project/root/node_modules/parcel/node_modules/@parcel/core/node_modules/@parcel/workers/lib/Worker.js:165:12)
    at /path/to/monorepo/project/root/node_modules/parcel/node_modules/@parcel/core/node_modules/@parcel/workers/lib/Worker.js:133:12
    at new Promise (<anonymous>)
    at Worker.sendSharedReference (/path/to/monorepo/project/root/node_modules/parcel/node_modules/@parcel/core/node_modules/@parcel/workers/lib/Worker.js:132:12)
    at /path/to/monorepo/project/root/node_modules/parcel/node_modules/@parcel/core/node_modules/@parcel/workers/lib/Worker.js:122:20
    at Array.map (<anonymous>)
    at Worker.fork (/path/to/monorepo/project/root/node_modules/parcel/node_modules/@parcel/core/node_modules/@parcel/workers/lib/Worker.js:121:81)
DOMException [DataCloneError]: function fn(...args) {
    return new Promise((resolve, reject) => {
      original.call(this, ...args, (err, ....<omitted>... } could not be cloned.
    at Worker.postMessage (internal/worker.js:340:23)
    at ThreadsWorker.send (/path/to/monorepo/project/root/node_modules/parcel/node_modules/@parcel/core/node_modules/@parcel/workers/lib/threads/ThreadsWorker.js:74:17)
    at Worker.send (/path/to/monorepo/project/root/node_modules/parcel/node_modules/@parcel/core/node_modules/@parcel/workers/lib/Worker.js:145:17)
    at Worker.call (/path/to/monorepo/project/root/node_modules/parcel/node_modules/@parcel/core/node_modules/@parcel/workers/lib/Worker.js:165:12)
    at /path/to/monorepo/project/root/node_modules/parcel/node_modules/@parcel/core/node_modules/@parcel/workers/lib/Worker.js:133:12
    at new Promise (<anonymous>)
    at Worker.sendSharedReference (/path/to/monorepo/project/root/node_modules/parcel/node_modules/@parcel/core/node_modules/@parcel/workers/lib/Worker.js:132:12)
    at /path/to/monorepo/project/root/node_modules/parcel/node_modules/@parcel/core/node_modules/@parcel/workers/lib/Worker.js:122:20
    at Array.map (<anonymous>)
    at Worker.fork (/path/to/monorepo/project/root/node_modules/parcel/node_modules/@parcel/core/node_modules/@parcel/workers/lib/Worker.js:121:81)
lerna ERR! npm run build exited 1 in '@mbhc/forms-core'

@WesleyMConner
Copy link
Author

@mischnic:

I ran into the same DOMException noted by @sepsol when testing "parcel": "2.0.0-nightly.945" on my small bug-demonstration project. As a gut check, I jumped over to a working parcel project and replaced "parcel": ">=2.0.1" with "parcel": "2.0.0-nightly.945". There too, I ran into the DOMException issue only when using ..945.

At this point, I stopped. My best guess was that some other issue may be impacting "2.0.0-nightly.945" (i.e., unrelated to this thread).

@mischnic
Copy link
Member

Can someone post a full reproduction (zip/git repo) including a lockfile? It was working for me

@WesleyMConner
Copy link
Author

Let me know if this helps. The package.json uses the nightly release and produced the included package-lock.json.

See package.json-original, which produces the fs/... issue.

parcel-bug-report.zip

@mischnic
Copy link
Member

This happened because you have multiple versions of various Parcel packages installed:

Bildschirmfoto 2021-12-15 um 21 57 31

@WesleyMConner
Copy link
Author

I tear down node_modules per build and only install Parcel. Is the nightly version picking up the dependencies (i.e., across other nightly versions)?

@sepsol
Copy link

sepsol commented Dec 16, 2021

Can someone post a full reproduction (zip/git repo) including a lockfile? It was working for me

@mischnic I've attached a minimal reproducible monorepo example:
parcel-monorepo-with-fs-issue.zip

UPDATE
My npm ls @parcel/cache results are similar, even though I removed node_modules and .parcel-cache:

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants