Node.js TypeScript在同一文件夹中找不到js模块
我正在努力加载一个与我的.ts文件位于同一文件夹中的.js模块。我在同一文件夹中有4个文件: index.tsNode.js TypeScript在同一文件夹中找不到js模块,node.js,typescript,Node.js,Typescript,我正在努力加载一个与我的.ts文件位于同一文件夹中的.js模块。我在同一文件夹中有4个文件: index.ts /// <reference path="./node.d.ts" /> /// <reference path="./foo.d.ts" /> import foo = require('./foo.js'); declare module "foo" { export function hello(): void; } module.expor
/// <reference path="./node.d.ts" />
/// <reference path="./foo.d.ts" />
import foo = require('./foo.js');
declare module "foo" {
export function hello(): void;
}
module.exports = {
hello: function() {
console.log('hello');
}
};
export function hello(): void;
/// <reference path="./node.d.ts" />
/// <reference path="./foo.d.ts" />
import foo = require('./foo');
foo.js
/// <reference path="./node.d.ts" />
/// <reference path="./foo.d.ts" />
import foo = require('./foo.js');
declare module "foo" {
export function hello(): void;
}
module.exports = {
hello: function() {
console.log('hello');
}
};
export function hello(): void;
/// <reference path="./node.d.ts" />
/// <reference path="./foo.d.ts" />
import foo = require('./foo');
运行tsc index.ts--module commonjs
时,出现以下错误:
index.ts(4,22): error TS2307: Cannot find module './foo.js'.
尝试使用不带相对路径的require
var foo = require('foo');
有关详细信息,请参见相关信息。由于node.js将通过相对路径解析
foo
,而不是像通过npm安装的模块那样在node\u modules
目录中查找它,因此需要在foo.d.ts
中删除声明模块“foo”
。另外,在index.ts
中,调用require
时,请删除.js
扩展名
foo.d.ts
/// <reference path="./node.d.ts" />
/// <reference path="./foo.d.ts" />
import foo = require('./foo.js');
declare module "foo" {
export function hello(): void;
}
module.exports = {
hello: function() {
console.log('hello');
}
};
export function hello(): void;
/// <reference path="./node.d.ts" />
/// <reference path="./foo.d.ts" />
import foo = require('./foo');
index.ts
/// <reference path="./node.d.ts" />
/// <reference path="./foo.d.ts" />
import foo = require('./foo.js');
declare module "foo" {
export function hello(): void;
}
module.exports = {
hello: function() {
console.log('hello');
}
};
export function hello(): void;
/// <reference path="./node.d.ts" />
/// <reference path="./foo.d.ts" />
import foo = require('./foo');
//
///
导入foo=require('./foo');