简体   繁体   English

编译成CommonJS不会产生NodeJS可用的文件

[英]Compiling into CommonJS does not produce files usable by NodeJS

Given the syntax provided here (standard ES6 modules with import/export), and the files below, the typescript compiler (tsc) builds files which will throw Error: Cannot find module when used with NodeJS. 鉴于此处提供的语法(带导入/导出的标准ES6模块)和下面的文件,typescript编译器(tsc)构建将抛出Error: Cannot find module文件Error: Cannot find module与NodeJS一起使用时Error: Cannot find module

Here's a stripped down example: 这是一个精简的例子:


Input files 输入文件

src/main.ts SRC / main.ts

import {Example} from 'example';
let e = new Example();

src/example.ts SRC / example.ts

export class Example {}

Compilation 汇编

Note, using the tsc from npm install -g typescript , version 1.5.0-beta , from a windows machine. 注意,使用来自windows机器的npm install -g typescript ,版本1.5.0-beta的tsc。

tsc --rootDir src --outDir bin

Output files 输出文件

bin/main.js 斌/ main.js

var example_1 = require('example');
var e = new example_1.Example();

bin/example.js 斌/ example.js

var Example = (function () {
    function Example() {
    }
    return Example;
})();
exports.Example = Example;

Am I doing something incorrectly? 我做错了什么吗? I expected main.js to include something like require('example.js') instead. 我希望main.js包含类似require('example.js')

 import {Example} from 'example'; 

You're importing a module here, not a file. 您在这里导入模块,而不是文件。 Try 尝试

import {Example} from './example';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM