parcel icon indicating copy to clipboard operation
parcel copied to clipboard

@parcel/validator-typescript error about type definitions for node despite having @types/node installed.

Open tagisgame opened this issue 1 year ago • 7 comments

🐛 bug report

I'm using TypeScript and Parcel and its @parcel/validator-typescript. I'm trying to get process.env.NODE_ENV variable and it keeps displaying that error: Cannot find name 'process'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node`., but I've got it installed, and I've searched for different solutions and I think I have all configurations done.

It's worth mentioning, that if I disable @parcel/validator-typescript the code runs without any issues.

🎛 Configuration

tsconfig.json

{
  "complierOptions": {
      "target": "es6",
      "strict": true,
      "typeRoots": [ "node_modules/@types" ],
      "moduleResolution": "node"
  },
  "include": ["src/ts/*"],
  "exclude": ["node_modules"]
}

package.json

...
  "devDependencies": {
      "@parcel/validator-typescript": "^2.6.2",
      "@types/node": "^18.0.0",
      "parcel": "^2.6.2",
      "parcel-reporter-static-files-copy": "^1.3.4",
      "typescript": "^4.7.4"
    },
...

.parcelrc

{
  "extends": ["@parcel/config-default"],
  "reporters":  ["...", "parcel-reporter-static-files-copy"],
  "validators": {
    "*.{ts,tsx}": ["...", "@parcel/validator-typescript"]
  }
}

🤔 Expected Behavior

Type definition for process should be recognized and error should not be displayed by validator.

😯 Current Behavior

Validator shows error: Cannot find name 'process'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node`. and halts the process.

💁 Possible Solution

🔦 Context

As I've said earlier in the general description.

💻 Code Sample

Any code with process.env.NODE_ENV

🌍 Your Environment

Software Version(s)
Parcel 2.6.2
Node v14.13.1
npm/Yarn 6.14.8
Operating System Windows 10
Typescript 4.7.4

tagisgame avatar Jul 02 '22 22:07 tagisgame

Try running the regular Typescript CLI (e.g. something like tsc --noEmit --isolatedModules), if that also shows an error, then it's still a configuration problem. You might also need to specify "types": [ "node" ], in the tsconfig

mischnic avatar Jul 03 '22 08:07 mischnic

Try running the regular Typescript CLI (e.g. something like tsc --noEmit --isolatedModules)

As I've mentioned above, it takes only disabling the validator (removing "validators": {"*.{ts,tsx}": ["...", "@parcel/validator-typescript"]}) to make it work. I've tried running tsc and it compiled into js, displaying no errors, yet I had to get rid of --isolatedModules, because my file wasn't a module but a script. It shouldn't change anything though.

You might also need to specify "types": [ "node" ], in the tsconfig

I tested that too, and it didn't help at all.

tagisgame avatar Jul 03 '22 09:07 tagisgame

You're right. Looks like a workaround is adding "./node_modules/@types/node/index.d.ts" to include.

mischnic avatar Jul 03 '22 09:07 mischnic

You're right. Looks like a workaround is adding "./node_modules/@types/node/index.d.ts" to include.

That helped. Also I had to, obviously, remove "node modules" from exclude, and it works for now. I hope I won't need that exluded later on 😬

tagisgame avatar Jul 03 '22 10:07 tagisgame

FWIW I was having a very similar issue. The only thing that reliably alleviated this was adding "types": [ "node" ] to my tsconfig.json. Maybe Parcel is using some custom configuration for this (or a related) field which overrides the desired default behaviour?

EDIT: I have now noticed that what I thought was just @parcel/validator-typescript is for some errors in my case also related to @parcel/transformer-typescript-types which also throws errors related to imports, as well as some general Typescript errors, like implicit any variables.

phippg avatar Jul 11 '22 13:07 phippg

This is the relevant code, I'm no expert at tsc either:

https://github.com/parcel-bundler/parcel/blob/99cf50510995112722f76f115dc7f2fd21587f34/packages/validators/typescript/src/TypeScriptValidator.js#L109-L140

ParseConfigHost and LanguageServiceHost are defined here: https://github.com/parcel-bundler/parcel/tree/v2/packages/utils/ts-utils/src

mischnic avatar Jul 11 '22 13:07 mischnic

It seems the issue I was experiencing with @parcel/transformer-typescript-types surfaces through this part: https://github.com/parcel-bundler/parcel/blob/99cf50510995112722f76f115dc7f2fd21587f34/packages/transformers/typescript-types/src/TSTypesTransformer.js#L74-L80

If I remove the "types": "dist/types.d.ts" from package.json no errors surface whatsoever. Looking at the options set as default in the transformer I don't see anything that would indicate my previous suspicion

https://github.com/parcel-bundler/parcel/blob/99cf50510995112722f76f115dc7f2fd21587f34/packages/transformers/typescript-types/src/TSTypesTransformer.js#L23-L41

phippg avatar Jul 11 '22 14:07 phippg

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 14 days if no further activity occurs.

github-actions[bot] avatar Jan 08 '23 00:01 github-actions[bot]

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 14 days if no further activity occurs.

github-actions[bot] avatar Jul 21 '23 12:07 github-actions[bot]