简体   繁体   English

避免在已编译的javascript中使用require语句

[英]Avoid require statement in the transpiled javascript

The following statements from a .ts file: .ts文件中的以下语句:

import {jquery as $} from 'jquery';
import {socketio} from 'socket.io';

get transpiled to: 变成:

var jquery_1 = require("jquery");
var socket_io_1 = require("socket.io");

Now the browser won't recognize require . 现在浏览器无法识别require What is the way out? 出路是什么? My tsconfg.json looks like: 我的tsconfg.json看起来像:

{
    "compilerOptions": {
        "target": "es5",
        "outDir": "./dist",
        "allowJs": true,
        "module": "commonjs",
        "noEmitOnError": false,
        "noImplicitAny": false,
        "strictNullChecks": true
    },
    "include": [
        "index.ts",
        "./lib/**/*",
        "./public/**/*"
    ]
}

Is there a need to change something? 是否需要改变一些东西?

I used browserify to bundle the final file into a browser compatible javascript. 我使用browserify将最终文件捆绑到浏览器兼容的javascript中。

browserify input.js -o output.js

There could be errors thrown by the browserify saying for example Error: Cannot find module 'bufferutil' from .. In that case, the respective modules could be installed from the npm. 可能会出现浏览器抛出的Error: Cannot find module 'bufferutil' from .. ,例如Error: Cannot find module 'bufferutil' from ..在这种情况下,可以从npm安装相应的模块。

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

相关问题 扩展 JavaScript `require` 语句 - Expanding JavaScript `require` statement ES6中的JavaScript范围已转换 - JavaScript Scope in ES6 transpiled 如何用Javascript编写import语句而不是require语句? - How to write import statement instead of require statement in Javascript? Transpiled Webpack软件包不会通过require导出带连字符的软件包名称 - Transpiled webpack bundle does not export hyphenated package name via require babel编译打字稿文件后,要求路径不正确 - Require paths are not proper after typescript file was transpiled by babel 我们如何避免Babel编译的文件中的样板代码? - How do we avoid boilerplate code in files transpiled by Babel? 包含 require() 语句的外部 JavaScript 库无法加载到 angular 8 应用程序中 - External JavaScript Library containing require()-statement failed to load into angular 8 app 在未转译的HTML页面中使用从Babel转译的Javascript文件导出的函数 - Using an exported function from a Babel-transpiled Javascript file inside a non-transpiled HTML page javascript function 什么时候需要在require语句后加空括号? - When does the javascript function need empty parenthesis after require statement? 为什么我的子功能需要一个“ return”语句以避免出错? 父函数没有“返回”且没有错误 - Why does my child function require a 'return' statement to avoid erroring out? The parent function has no 'return' and no error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM