Nút babel

Lỗi này chỉ xảy ra khi bạn sử dụng async/await trong mã của mình. Sau đó, sử dụng polyfill bao gồm thời gian chạy trình tạo lại tùy chỉnh và core-js. thêm vào đầu trang của

 require = require("esm")(module/*, options*/)
 // Import the rest of our application.
 module.exports = require('./src/server.js')
 // where server.js is express server start file
2

import "babel-polyfill"

Điều này cho phép bạn sử dụng async/await

sử dụng Babel 7

Cần cập nhật mọi thứ trong dự án của bạn, hãy bắt đầu với babel 7. babelrc

  yarn add esm / npm install esm
0

Một số thay đổi trong gói. json

  yarn add esm / npm install esm
1

và sử dụng

 require = require("esm")(module/*, options*/)
 // Import the rest of our application.
 module.exports = require('./src/server.js')
 // where server.js is express server start file
3 trên điểm bắt đầu

  yarn add esm / npm install esm
2

Bạn đang nghĩ tại sao

 require = require("esm")(module/*, options*/)
 // Import the rest of our application.
 module.exports = require('./src/server.js')
 // where server.js is express server start file
4

Nghiêm trọng. Đó là một câu hỏi hay nếu bạn là người mới. Mọi thay đổi, bạn đều sử dụng máy chủ khởi động mỗi lần, sau đó sử dụng

 require = require("esm")(module/*, options*/)
 // Import the rest of our application.
 module.exports = require('./src/server.js')
 // where server.js is express server start file
5 làm máy chủ phát triển, mọi thay đổi đều tự động khởi động lại máy chủ để biết thêm về gật đầu

Lỗi cú pháp. Nhập mã thông báo không mong muốn trong Node. js #

"Lỗi cú pháp. Nhập mã thông báo không mong muốn" xảy ra khi chúng tôi sử dụng cú pháp nhập ES6 trong phiên bản Nút không hỗ trợ nó. Để giải quyết lỗi, hãy sử dụng cú pháp

 require = require("esm")(module/*, options*/)
 // Import the rest of our application.
 module.exports = require('./src/server.js')
 // where server.js is express server start file
6, e. g.
 require = require("esm")(module/*, options*/)
 // Import the rest of our application.
 module.exports = require('./src/server.js')
 // where server.js is express server start file
7 hoặc đặt thuộc tính
 require = require("esm")(module/*, options*/)
 // Import the rest of our application.
 module.exports = require('./src/server.js')
 // where server.js is express server start file
8 thành
 require = require("esm")(module/*, options*/)
 // Import the rest of our application.
 module.exports = require('./src/server.js')
 // where server.js is express server start file
9 trong tệp
import "babel-polyfill"
20 của bạn

Đây là một ví dụ về cách xảy ra lỗi

  yarn add esm / npm install esm
3

Để giải quyết lỗi, hãy thay thế nhập mô-đun ES6 của bạn bằng nhập

 require = require("esm")(module/*, options*/)
 // Import the rest of our application.
 module.exports = require('./src/server.js')
 // where server.js is express server start file
6 được hỗ trợ trong phiên bản Nút của bạn. js

  yarn add esm / npm install esm
4

Sử dụng cú pháp mô-đun yêu cầu CommonJS sẽ giải quyết lỗi

Nếu bạn thích sử dụng cú pháp mô-đun ES6, hãy đặt thuộc tính

 require = require("esm")(module/*, options*/)
 // Import the rest of our application.
 module.exports = require('./src/server.js')
 // where server.js is express server start file
8 thành
 require = require("esm")(module/*, options*/)
 // Import the rest of our application.
 module.exports = require('./src/server.js')
 // where server.js is express server start file
9 trong tệp
import "babel-polyfill"
20 của bạn

  yarn add esm / npm install esm
5

Nếu bạn không có tệp

import "babel-polyfill"
20, hãy khởi tạo một tệp bằng cách sử dụng lệnh
import "babel-polyfill"
26 trong thư mục gốc của dự án của bạn

Bây giờ bạn có thể sử dụng cú pháp mô-đun ES6 trong Nút của mình. ứng dụng js

  yarn add esm / npm install esm
6

Khi bạn nhập các tệp cục bộ và đã đặt thuộc tính

 require = require("esm")(module/*, options*/)
 // Import the rest of our application.
 module.exports = require('./src/server.js')
 // where server.js is express server start file
8 thành
 require = require("esm")(module/*, options*/)
 // Import the rest of our application.
 module.exports = require('./src/server.js')
 // where server.js is express server start file
9, bạn phải bao gồm phần mở rộng
import "babel-polyfill"
29

  yarn add esm / npm install esm
7

Nếu bạn bỏ qua phần mở rộng, bạn sẽ gặp lỗi - "Lỗi [ERR_MODULE_NOT_FOUND]. Không thể tìm thấy mô-đun X"

"Lỗi cú pháp. Nhập mã thông báo không mong muốn" cũng xảy ra nếu cố gắng chạy các tệp nguồn chứa cú pháp mô-đun ES6

  yarn add esm / npm install esm
70 /
  yarn add esm / npm install esm
71, thay vì chạy các tệp đã biên dịch từ thư mục bản dựng của bạn

Đảm bảo chỉ chạy các tệp đã biên dịch từ thư mục build/dist của bạn

Phần kết luận #

"Lỗi cú pháp. Nhập mã thông báo không mong muốn" xảy ra khi chúng tôi sử dụng cú pháp nhập ES6 trong phiên bản Nút không hỗ trợ nó. Để giải quyết lỗi, hãy sử dụng cú pháp

 require = require("esm")(module/*, options*/)
 // Import the rest of our application.
 module.exports = require('./src/server.js')
 // where server.js is express server start file
6, e. g.
 require = require("esm")(module/*, options*/)
 // Import the rest of our application.
 module.exports = require('./src/server.js')
 // where server.js is express server start file
7 hoặc đặt thuộc tính
 require = require("esm")(module/*, options*/)
 // Import the rest of our application.
 module.exports = require('./src/server.js')
 // where server.js is express server start file
8 thành
 require = require("esm")(module/*, options*/)
 // Import the rest of our application.
 module.exports = require('./src/server.js')
 // where server.js is express server start file
9 trong tệp
import "babel-polyfill"
20 của bạn

Làm cách nào để khắc phục lỗi cú pháp mã thông báo không mong muốn?

Để giải quyết "Uncaught SyntaxError. Lỗi định danh không mong đợi", đảm bảo bạn không có bất kỳ từ khóa sai chính tả nào, e. g. Let hoặc Function thay vì let và function và sửa bất kỳ lỗi chính tả nào liên quan đến thiếu hoặc thừa dấu phẩy, dấu hai chấm, dấu ngoặc đơn, dấu ngoặc kép hoặc dấu ngoặc vuông