Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Why doesn't nodejs support import?

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "Why nodejs does not support import". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn why nodejs does not support import.

Nodejs does not support import statements because nodejs uses the modular specification of CommonJS and introduces modules using the modular statement, while import is the modular specification keyword of ES6.

This tutorial operating environment: windows7 system, nodejs version 12.19.0, DELL G3 computer.

Nodejs does not support import statements.

Reason: nodejs uses the modular specification of CommonJS and introduces modules using require, while import is the modular specification keyword of ES6.

And sometimes you need to use import, what should I do?

If you want to use import, you must introduce babel escape support and compile it through babel to make it modular code for node.

The method of using import/export in node:

If you want to use import, you must introduce babel escape support and compile it through babel to make it modular code for node.

We can use it on the command line

Babel-node command to compile, babel-node command is not a separate installation, before the Babel 7.x version, you need to install babel-cli

The package is available; in Babel version 7.x, you need to install @ babel/core and @ babel/cli packages.

Note: this is an example of the version before 7.x. Version 7.x can go to the official website to find the use case of the corresponding package.

Install the necessary plug-ins

Global installation of babel-cli

Npm install babel-cli-g

Install babel-preset-env

Npm install babel-preset-env-D

And then it turned out to be node server.js. Instead, it called: babel-node-- presets env server.js.

It should be noted that if it is only for the babel-node command, installing babel-cli will load and install a lot of resources and modules, and it is not recommended for use in a production environment for performance reasons. When you are developing and debugging, you can play with it.

Thank you for your reading, the above is the content of "Why nodejs does not support import". After the study of this article, I believe you have a deeper understanding of why nodejs does not support import, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report