Generates invalid package.json file when referencing css in parent directory

🐛 bug report

Given a directory structure where the parcel project resides in a subdirectory. When referencing a css file anywhere in the parent directory structure, parcel overwrites the package.json file in the subdirectory completely with a file containing exclusively devDependencies with a single entry for cssnano. This does not happen if --no-minification is specified on the command line.

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

package.json:

{
  "name": "parcel-project",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "build": "parcel build index.js"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "parcel-bundler": "^1.12.3"
  }
}

index.js:

import '../test.css';

command:

$ npm run build

🤔 Expected Behavior

Build should produce css and js file in dist directory.

😯 Current Behavior

$ npm run build

> parcel-project@1.0.0 build /tmp/bundler-test/parcel-project
> parcel build index.js

🚨  /tmp/bundler-test/test.css:undefined:undefined: Cannot find module 'cssnano' from '/tmp/bundler-test'
    at /tmp/bundler-test/parcel-project/node_modules/resolve/lib/async.js:68:35
    at processDirs (/tmp/bundler-test/parcel-project/node_modules/resolve/lib/async.js:208:39)
    at ondir (/tmp/bundler-test/parcel-project/node_modules/resolve/lib/async.js:223:13)
    at load (/tmp/bundler-test/parcel-project/node_modules/resolve/lib/async.js:101:43)
    at onex (/tmp/bundler-test/parcel-project/node_modules/resolve/lib/async.js:126:17)
    at /tmp/bundler-test/parcel-project/node_modules/resolve/lib/async.js:13:69
    at FSReqWrap.oncomplete (fs.js:153:21)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! parcel-project@1.0.0 build: `parcel build index.js`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the parcel-project@1.0.0 build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     ~/.npm/_logs/2019-04-02T13_14_45_773Z-debug.log

Also contents of package.json looks like this afterwards:

{
  "devDependencies": {
    "cssnano": "^4.1.10"
  }
}

💁 Possible Solution

?

🔦 Context

💻 Code Sample

Repository with reproduction: znerol-scratch/parcel-parent-dir-bug

🌍 Your Environment

Software Version(s)
Parcel 1.12.3
Node 10.15.3
npm/Yarn npm 6.4.1
Operating System Linux

1 possible answer(s) on “Generates invalid package.json file when referencing css in parent directory

  1. it’s 2020.12 and saw same issues here, could not get parcel work with bootstrap 5 when building for production, this is a critical bug for me and I had to switch to webpack.