问题
I just started a new nodejs project using typescript. I installed Typings (https://github.com/typings/typings) and used that to install reference files for node v4.x and express v4.x.
My node version is v4.2.6 My typescript version is v1.7.5
My project directory is laid out thus:
package.json
tsconfig.json
typings.json
src/
app.ts
typings/
main.d.ts
main/ambient/node/node.d.ts
main/ambient/express/express.d.ts
The contents of typings/main.d.ts are as follows:
/// <reference path="main/ambient/express/express.d.ts" />
/// <reference path="main/ambient/node/node.d.ts" />
The contents of tsconfig.json are as follows:
{
"compilerOptions": {
"target": "es6",
"module": "commonjs"
}
}
The contents off typings.json are as follows:
{
"dependencies": {},
"devDependencies": {},
"ambientDependencies": {
"express": "github:DefinitelyTyped/DefinitelyTyped/express/express.d.ts#dd4626a4e23ce8d6d175e0fe8244a99771c8c3f2",
"node": "github:DefinitelyTyped/DefinitelyTyped/node/node.d.ts#1c56e368e17bb28ca57577250624ca5bd561aa81"
}
}
The contents of src/app.ts are as follows:
'use strict';
///<reference path="../typings/main.d.ts"/>
import * as express from "express";
This is exceedingly simple and should result in a basic app. However, when I try to compile this I get the error error TS2307: Cannot find module 'express'.
I have tried rearranging typings files, changing the relative path in the reference path tag, using the files
field in tsconfig.json to indicate the reference paths instead of using an inline tag in the file, all to no avail. I have also tried compiling using gulp-typescript
, gulp-tsc
, and tsc
directly on the command line.
I get similar errors when I try to use nodejs build-in modules such as crypto
, http
, fs
etc.
These references seem valid -- what am I missing?
回答1:
Triple-slash directives need to precede any statements in your file. Your "use strict"
prologue needs to come after your reference comments as so:
///<reference path="../typings/main.d.ts"/>
'use strict';
import * as express from "express";
As a follow up to your comment where you're not getting any emit for your import: that's because TypeScript performs import elision. Unless you use the module for some value, the compiler won't emit the import because it assumes you only need that module for its types.
回答2:
The answer by Daniel is technically correct but based on your tsconfig main.d.ts
would have still been picked up so would not fix issues for you.
That said I did find issues and sent a pull request: https://github.com/jereynolds/ts-test/pull/1
You probably should exclude
typings
andnode_modules
otherwise the compile will contain duplicate identifiers (typings) / will be slow (node_modules)typings install
serve-static
(needed forexpress
) andmime
(needed forserve-static
).
回答3:
I know this is an old question, and sorry for bumping it but it comes up as one of the first links in Google.
Since the above didn't fix my issue and I eventually resolved it, I thought I'd share... In my case, it was relative pathing that caused the issue.
My typings were in
typings/bootstrap
,
typings/react-dom
,
typings/react
,
etc.
The react-dom
one has an import from 'react'
. To fix my code I had to edit it to be '../react/react'
. Simple!
回答4:
None of the above worked for me - so I hope this will help someone else. (I'm using VS Code).
This issue suddenly happened to me out of the blue with one of my services, which was referenced in several modules. After saving a completely non-related component that DIDN'T use the service, two of the components using this service reported the 'cannot find module' error. I was able to fix it by simply changing the declaration to an invalid folder, saving the file and then changing it back to the correct path.
来源:https://stackoverflow.com/questions/35122246/typescript-cannot-find-module-with-valid-typings