parcel-bundler / parcel

The zero configuration build tool for the web. 📦🚀
https://parceljs.org
MIT License
43.37k stars 2.26k forks source link

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

Open zhfnjust opened 1 month ago

zhfnjust commented 1 month ago

🐛 bug report

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

🎛 Configuration (.babelrc, package.json, cli command)

{
  "name": "@scrypt/ssapp",
  "version": "1.0.0",
  "source": "src/index.js",
  "main": "dist/main.js",
  "module": "dist/module.js",
  "browser": "dist/browser.js",
  "types": "index.d.ts",
  "alias": {
    "process": "process/browser.js",
    "assert": "assert",
    "crypto": "crypto-browserify"
  },
  "targets": {
    "types": false
  },
  "scripts": {
    "watch": "parcel watch",
    "build": "parcel build --no-cache",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "description": "",
  "dependencies": {
    "assert": "^2.1.0",
    "crypto-browserify": "^3.12.0",
    "path-browserify": "^1.0.1",
    "process": "^0.11.10"
  },
  "devDependencies": {
    "parcel": "^2.12.0"
  }
}

🤔 Expected Behavior

Build success.

😯 Current Behavior

Build failed.

@parcel/core: Failed to resolve 'crypto' from './src/index.js'

C:\Users\myland\code\tmp\ssApp\src\index.js:2:28 1 | import { join } from "path";

2 | import { createHash } from 'crypto' | ^^^^^^^^ 3 | import { readFileSync } from 'fs' 4 | import assert from "assert";

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

C:\Users\myland\code\tmp\ssApp\package.json:26:3 25 | "description": "",

26 | "dependencies": { | ^^^^^^^^^^^^^^ 27 | "assert": "^2.1.0", 28 | "crypto-browserify": "^3.12.0",

💡 Add "crypto" as a dependency.

💁 Possible Solution

🔦 Context

💻 Code Sample

import { join } from "path";
import { createHash } from 'crypto'
import { readFileSync } from 'fs'
import assert from "assert";
export function add(a, b) {
    return a + b;
}

export function getaa() {
    return join("/asdfasdf/adfasdf", 'asdfas');
}

export function myhash(str) {
    assert.deepEqual(str, "aa")
    console.log('myhash', str, process.env)
    console.log('aaa', readFileSync(join("aa", "aa.txt")).toString())
    return createHash('sha256').update(str).digest('hex');
}

export default {
    add,
    getaa,
    myhash
}

🌍 Your Environment

Software Version(s)
Parcel 2.12.0
Node v18.17.0
npm/Yarn npm 9.6.7
Operating System windows
zhfnjust commented 1 month ago

this error only happened when i use parcel version >= v2.9.0