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
2import "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
0Một số thay đổi trong gói. json
yarn add esm / npm install esm
1và 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
2Bạ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
4Nghiê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 đầuLỗ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
4Sử 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
5Nếu bạn không có tệp
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ạnimport "babel-polyfill"
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
6Khi 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
7Nế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ạnLà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