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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serve with Parcel 2 throw error "Cannot read property 'env' of undefined" #7267

Closed
michaelbrylevskii opened this issue Nov 8, 2021 · 1 comment

Comments

@michaelbrylevskii
Copy link

michaelbrylevskii commented Nov 8, 2021

馃悰 bug report

When I run npx parcel serve --target frontend, I get the error "Cannot read property 'env' of undefined". But watch or build works fine.

PS D:\Projects\_TEST\TsFullStack> npx parcel serve --target frontend
Server running at http://localhost:1234
脳 Build failed.

Error: Cannot read property 'env' of undefined

  TypeError: Cannot read property 'env' of undefined
  at TargetResolver.resolve (D:\Projects\_TEST\TsFullStack\node_modules\@parcel\core\lib\requests\TargetRequest.js:281:56)
  at async Object.run (D:\Projects\_TEST\TsFullStack\node_modules\@parcel\core\lib\requests\TargetRequest.js:145:17)
  at async RequestTracker.runRequest (D:\Projects\_TEST\TsFullStack\node_modules\@parcel\core\lib\RequestTracker.js:721:20)
  at async AssetGraphBuilder.runTargetRequest (D:\Projects\_TEST\TsFullStack\node_modules\@parcel\core\lib\requests\AssetGraphRequest.js:751:19)
  at async PromiseQueue._runFn (D:\Projects\_TEST\TsFullStack\node_modules\@parcel\utils\lib\PromiseQueue.js:88:7)
  at async PromiseQueue._next (D:\Projects\_TEST\TsFullStack\node_modules\@parcel\utils\lib\PromiseQueue.js:75:5)

馃帥 Configuration

Command: npx parcel serve --target frontend

Project Structure:

- dist
- - ...
- node_modules
- - ...
- packages
- - backend
- - - src
- - - - index.ts
- - common
- - - src
- - - - index.ts
- - frontend
- - - src
- - - - index.html
- - - - index.ts
- package.json
- package-lock.json
- tsconfig.json

package.json

{
  "name": "ts-full-stack",
  "version": "1.0.0",
  "description": "TypeScript full-stack application",
  "author": "chop",
  "dependencies": {
    "express": "^4.17.1"
  },
  "devDependencies": {
    "@types/express": "^4.17.13",
    "@types/node": "^16.9.3",
    "concurrently": "^6.2.1",
    "parcel": "^2.0.0",
    "typescript": "^4.4.3"
  },
  "targets": {
    "frontend": {
      "source": "packages/frontend/src/index.html",
      "distDir": "dist/frontend",
      "context": "browser",
      "engines": {
        "browsers": "> 5%, last 2 versions, not dead"
      },
      "includeNodeModules": true
    },
    "backend": {
      "source": "packages/backend/src/index.ts",
      "distDir": "dist/backend",
      "context": "node",
      "engines": {
        "node": ">= 14"
      },
      "includeNodeModules": true
    }
  },
  "scripts": {
    "watch:frontend": "parcel watch --target frontend",
    "watch:backend": "parcel watch --target backend",
    "build": "npm run build:backend & npm run build:frontend",
    "build:frontend": "parcel build --target frontend",
    "build:backend": "parcel build --target backend"
  }
}

tsconfig.json

{
  "compilerOptions": {
    "isolatedModules": true
  }
}

index.html in frontend

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script type="module" src="index.ts"></script>
</head>
<body>
    <h2>Hello World</h2>
</body>
</html>

index.ts in frontend

import { sayHi } from '../../common/src/index';

console.log("Frontend Module Loaded");
sayHi("Loki");

export {};

index.ts in common

console.log("Common Module Loaded");

export function sayHi(user) {
  alert(`Hello, ${user}!`);
}

馃實 Your Environment

Software Version(s)
Parcel 2.0.0
Node 14.17.6
npm 7.5.4
Operating System Windows 10
@mischnic
Copy link
Member

mischnic commented Nov 8, 2021

Duplicate of #6905. Will be fixed in the next release: #7187

@mischnic mischnic closed this as completed Nov 8, 2021
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

2 participants