当前位置:网站首页>import和require在浏览器和node环境下的实现差异
import和require在浏览器和node环境下的实现差异
2022-06-22 02:28:00 【望屿】
1. import
import属于esmodule规范的内容,具体的esmodule和commonjs模块规范可以看我的这篇博客
1. 浏览器不支持import
- 解决办法:
给script标签的type属性规定为module即可
<script src="xxx" type="module"></script>
2. node环境不支持import
举个栗子:
目前的目录结构如下:
|-src
|-a.js
|-b.js
- src/a.js:
import {
name } from './b.js'
console.log(name);
- src/b.js
export const name = '望屿'
现在在node环境下执行,输入命令node a.js,报错:
import {
name } from './b.js'
^^^^^^
SyntaxError: Cannot use import statement outside a module
解决方法:
|-src
|-a.mjs
|-b.mjs
- 总结为一句话:.mjs文件总是以 ES6 模块加载,.cjs文件总是以 CommonJS 模块加载,.js文件的加载取决于package.json里面type字段的设置。
- 安装以下三个babel相关插件(分别代表babel命令行工具,babel核心转换功能,babel对于高级js语法的转换预设),我安装的是7.0版本
cnpm i -D @babel/cli @babel/core @babel/preset-env
- 在项目终端输入以下命令:
表示把src目录下的所有文件编译到lib目录下,并用上env这个预设
babel src -d lib --presets=@babel/preset-env
- 编译完成后,项目结构如下:
|-src
|-a.js
|-b.js
|-lib
|a.js
|b.js
- 现在我们运行 lib/a.js (
node lib/a.js),成功输出“望屿”
2. require
1. node环境支持require
既然支持那就很好,略了
2. 浏览器环境不支持require
- 举个栗子:
目前的项目结构
|-src
|-a.js
|-b.js
|-index.html
- index.html:
<!DOCTYPE html>
<html lang="en">
<body>
<script src="./src/a.js"></script>
</body>
</html>
- ./src/a.js
let {
name } = require('./b.js')
console.log(name);
- ./src/b.js
module.exports.name = '望屿'
解决方法:
方法1:使用webpack等前端构建工具进行转换
- 全局安装webpack和webpack-cli
cnpm i -g webpack webpack-cli
- 配置webpack.config.js文件,规定入口和转换后的出口路径
const path = require('path')
module.exports = {
entry: './src/a.js',
output: {
path: path.resolve(__dirname, './lib'),
filename: 'build.js'
}
}
- 终端输入命令
$ webpack --config webpack.config.js --mode production
- 目前的目录结构变成了这样:
|-src
|-a.js
|-b.js
|-lib
|-build.js
|-index.html
- 修改index.html文件的src引入路径
<script src="./lib/build.js"></script>
- 打开浏览器,可以看到正确输出name“望屿”了
方法2:使用browserfify
- 安装
cnpm i -D browserify
- 使用它对文件进行转换
browserify .\src\a.js > .\lib\build.js
- 修改index.html的script标签的引入,成功在浏览器运行~
方法3:使用AMD规范(require.js)
require.js是一个非常流行的库,是对AMD规范的实现,这个方法会稍微麻烦一点,这里就不展开了,感兴趣的可以看阮一峰老师的博客,有对AMD规范和require.js用法的详细说明
边栏推荐
- 带你区分几种并行
- Brief introduction to common pigtails of communication pigtails
- Which Amazon evaluation system is better?
- Completion of graduation design of wechat small program film and television review and exchange platform system (5) assignment
- EMC辐射发射整改-原理案例分析
- LeetCode 513 找树左下角的值[BFS 二叉树] HERODING的LeetCode之路
- Creating and extending XFS file system based on LVM
- OpenJudge NOI 1.13 46:八进制到十进制
- EMC Radiation Emission rectification - principle Case Analysis
- Get to know unity3d (project structure, third-party plug-in of probuilder)
猜你喜欢

Creating and extending XFS file system based on LVM

Wechat applet film and television comment exchange platform system graduation design completion (6) opening defense ppt

微信小程序影视评论交流平台系统毕业设计毕设(1)开发概要

Development of power plant compliance test system with LabVIEW

Completion of graduation design of wechat small program film and television review and exchange platform system (4) opening report

Games-101 personal summary rasterization
![[phantom engine UE] package error appears! Solutions to findpin errors](/img/d5/6747e20da6a8a4ca461094bd27bbf0.png)
[phantom engine UE] package error appears! Solutions to findpin errors

Wechat applet film and television review and exchange platform system graduation design completion (8) graduation design thesis template

FPGA Xilinx 7 Series FPGA DDR3 hardware design rules

目标检测之——labelImg标注工具使用方法
随机推荐
cmake常用命令分类备忘
数组常用方法
excel常用快捷鍵excel快捷鍵匯總
The "cloud" end of the 6th world intelligence conference will be held soon
[essay] the Expo that studied the RN ecology for one day yesterday is really awesome. It works well from development and construction to deployment.
微信小程序影视评论交流平台系统毕业设计毕设(4)开题报告
Chapter 25 digital watermarking technology based on Wavelet Transform
微信小程序影视评论交流平台系统毕业设计毕设(7)中期检查报告
Development of power plant compliance test system with LabVIEW
PMP备考相关敏捷知识
What does informer have
Huayang smart rushes to Shenzhen Stock Exchange: it plans to raise 400million Fosun Weiying as a shareholder
New employees enter the company and learn about work information
Excel Common shortcut keys Excel shortcut keys Summary
2022 brazing test simulation 100 questions and answers
微信小程序影视评论交流平台系统毕业设计毕设(5)任务书
Completion of graduation design of wechat small program film and television review and exchange platform system (4) opening report
What does the maturity and redemption time of financial products mean?
OpenJudge NOI 1.13 46:八进制到十进制
Paper notes: multi label learning ackel