Parcel: TypeScript ```import '@types/handlebars';``` leads to "Cannot resolve dependency '@types/handlebars'"

Created on 22 Jan 2018  ยท  6Comments  ยท  Source: parcel-bundler/parcel

Choose one: ๐Ÿ› bug report

// index.ts
import '@types/handlebars';

parsel index.ts

X Cannot resolve dependency '@types/handlebars'

๐ŸŽ› Configuration (.babelrc, package.json, cli command)

Babel: no

๐Ÿค” Expected Behavior

The import command tells TypeScript to include a declaration file. Without this line any reference to Handlebars module are highlighted as invalid (PhpStorm). Even if the JS file for handlebars is imported with

import 'handlebars/dist/handlebars.min.js';

๐Ÿ˜ฏ Current Behavior

parcel entry.html
Server running at http://localhost:1234
ร— RenderOpportunities.ts:3:8: Cannot resolve dependency '@types/handlebars'
1 | 'use strict';
2 |
3 | import '@types/handlebars';
| ^

๐Ÿ’ Possible Solution

No idea.

๐Ÿ”ฆ Context

It's hard/impossible to continue working with parcel-bundler and Handlebars library with TypeScript which requires the declaration file to be imported.

๐Ÿ’ป Code Sample

See above.

๐ŸŒ Your Environment

parcel -V
1.4.1

node -v
v9.3.0

npm -v
5.6.0

yarn -v
1.3.2

ver
Microsoft Windows [Version 6.1.7601]

Thanks.

Bug Stale TypeScript

Most helpful comment

I created #587 to warn instead of throwing when a fs call cannot be statically evaluated, you can try it while it's being reviewed.

If it does not work I'm not aware of a resolve alias feature in Parcel, the TypeScript plugin supports TypeScript path rewriting using the paths compiler option, you can use it if the handlebars typings declares the handlebars/dist/handlebars.min.js module.

{
  "compilerOptions": {
    // ...
    "baseUrl": ".",
    "paths": {
      "handlebars": ["handlebars/dist/handlebars.min.js"]
     }
  }
}

All 6 comments

It may be related to https://github.com/Microsoft/TypeScript/issues/9725 but

> tsc index.ts

does not complain about anything.

It's due to the fact that Parcel doesn't use the TypeScript compiler but the the transpiler, it doesn't do import ellidation or any type-checking related transformations.

Solution:

  • Remove import '@types/handlebars' from index.ts
  • Add this to your compileOptions:
    js { "compilerOptions": { // ... "types": ["handlebars"] } }

Thank you. This helped a lot. I have a different (but related to handlebars) issue.

$ parcel entry.html
Server running at http://localhost:1234
ร—  ...\node_modules\handlebars\lib\index.js: Could not statically evaluate fs call

AFAIK, the issue is that handlebars module contains node fs call which is not available in the browser target.
Webpack has a workaround for this. They define an alias for 'handlebars' module which point to a different file.

Any suggestion for Parcel?

My import code

import * as Handlebars from 'handlebars';

Thanks.

I created #587 to warn instead of throwing when a fs call cannot be statically evaluated, you can try it while it's being reviewed.

If it does not work I'm not aware of a resolve alias feature in Parcel, the TypeScript plugin supports TypeScript path rewriting using the paths compiler option, you can use it if the handlebars typings declares the handlebars/dist/handlebars.min.js module.

{
  "compilerOptions": {
    // ...
    "baseUrl": ".",
    "paths": {
      "handlebars": ["handlebars/dist/handlebars.min.js"]
     }
  }
}

I got a similar error when I use custom paths:

__tsconfig__

"baseUrl": "./",
"paths": {
  "@src/*": [
    "src/*"
  ]
},

__output__

yarn build                                                                              (2s 663ms) โ”‚
yarn run v1.5.1
$ env NODE_ENV=production parcel build --no-source-maps --experimental-scope-hoisting --detailed-report --public-url ./
๐Ÿšจ  /home/joseluisq/myapp/src/main.tsx:2:37: Cannot resolve dependency '@src/app'
    at Resolver.resolve (/home/joseluisq/myapp/node_modules/parcel-bundler/src/Resolver.js:70:17)
    at <anonymous>
error An unexpected error occurred: "Command failed.

But in development mode curiously works.

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

devongovett picture devongovett  ยท  3Comments

philipodev picture philipodev  ยท  3Comments

466023746 picture 466023746  ยท  3Comments

oliger picture oliger  ยท  3Comments

donaldallen picture donaldallen  ยท  3Comments