The What's new in TypeScript wiki page and the Announcing TypeScript 2.0 Beta blog posting
both indicate that TypeScript 2.x supports Shorthand ambient module declarations, yet when I
attempt to use declare module 'jsonschema'; for the jsonschema npm package I get
error TS2664: Invalid module name in augmentation, module 'jsonschema' cannot be found.
There is a GitHub issue (#8518) that recommends getting the latest nightly build, but I
have the latest and still get the problem. How can this be resolved?
Clone repo
git clone https://github.com/clavecoder/bugreport-typescript-2-1-0-dev-20160714--invalid-module-name-in-augmentation.git
cd to repoInstall packages
npm install
Build project
npm run buildResult
error TS2664: Invalid module name in augmentation, module 'jsonschema' cannot be found.
error TS2307: Cannot find module 'jsonschema'.
Output
c:\git\clavecoder\bugreport-typescript-2-1-0-dev-20160714--invalid-module-name-in-augmentation>npm run build
> bugreport-typescript-2-1-0-dev-20160714--invalid-module-name-in-augmentation@0.0.0 build c:\git\clavecoder\bugreport-typescript-2-1-0-dev-20160714--invalid-module-name-in-augmentation
> tsc
modules/test-module/src/test.ts(1,16): error TS2664: Invalid module name in augmentation, module 'jsonschema' cannot be found.
modules/test-module/src/test.ts(3,23): error TS2307: Cannot find module 'jsonschema'.
npm ERR! Windows_NT 10.0.10586
npm ERR! argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "run" "build"
npm ERR! node v6.3.0
npm ERR! npm v3.10.3
npm ERR! code ELIFECYCLE
npm ERR! bugreport-typescript-2-1-0-dev-20160714--invalid-module-name-in-augmentation@0.0.0 build: `tsc`
npm ERR! Exit status 2
npm ERR!
npm ERR! Failed at the bugreport-typescript-2-1-0-dev-20160714--invalid-module-name-in-augmentation@0.0.0 build script 'tsc'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the bugreport-typescript-2-1-0-dev-20160714--invalid-module-name-in-augmentation package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! tsc
npm ERR! You can get information on how to open an issue for this project with:
npm ERR! npm bugs bugreport-typescript-2-1-0-dev-20160714--invalid-module-name-in-augmentation
npm ERR! Or if that isn't available, you can get their info via:
npm ERR! npm owner ls bugreport-typescript-2-1-0-dev-20160714--invalid-module-name-in-augmentation
npm ERR! There is likely additional logging output above.
npm ERR! Please include the following file with any support request:
npm ERR! c:\git\clavecoder\bugreport-typescript-2-1-0-dev-20160714--invalid-module-name-in-augmentation\npm-debug.log
c:\git\clavecoder\bugreport-typescript-2-1-0-dev-20160714--invalid-module-name-in-augmentation>
the declaration declare module 'jsonschema'; should be in a global scope. i.e. a top level declaration in a non module (where a module is a file with at least one top level import or export).
A declaration of the form declare module '...' { } in a module is an augmentation, see https://github.com/Microsoft/TypeScript-Handbook/blob/fa9e2be1024014fe923d44b1b69d315e8347e444/pages/Declaration%20Merging.md#module-augmentation for more details.
Thanks for the quick answer. Seems obvious in retrospect. Perhaps you could update the What's New documents or make sure it's included in the final documentation? It would also be nice if the error message were more helpful...
The answer was alluded to by the use of declarations.d.ts. Is that the recommended name? Where's the best place to put this file in 2.0? Are you still recommending the "typings" folder?
Ah, waste some much time to find out this issue. +1 for update What's New documents.
@muzuiget, Yeah, humdinger. To +1 use the new "add your reaction" feature in GitHub.
I am getting a similar error:
[ts] Invalid module name in augmentation. Module 'shiitake' resolves to an untyped module at 'd:/dev/foo/foobar.foo.Client.Web/node_modules/shiitake/dist/index.js', which cannot be augmented.
I have asked this on StackoverFlow, too: TypeScript custom declaration files for untyped npm modules
It would be nice if someone can shed some light on that.
@mhegazy Thanks for that link on augmentation, the errors really don't help much, until Googling landed me here. I ~am~ was having this problem: https://github.com/Microsoft/TypeScript/issues/4166#issuecomment-293481845 (solved).
Most helpful comment
the declaration
declare module 'jsonschema';should be in a global scope. i.e. a top level declaration in a non module (where a module is a file with at least one top levelimportorexport).A declaration of the form
declare module '...' { }in a module is an augmentation, see https://github.com/Microsoft/TypeScript-Handbook/blob/fa9e2be1024014fe923d44b1b69d315e8347e444/pages/Declaration%20Merging.md#module-augmentation for more details.