[英]How to use require-js on node.js without having to require(“requirejs”) and redefine requirejs in each file?
[英]How to use import and require in the same file - Node JS
我正在嘗試使用 normalize-url package 但它必須導入而不是必需,因為它在 ES 模塊中不受支持。 我試圖通過添加我在網上找到的兩行代碼來解決這個問題,這應該可以解決問題,但它對我不起作用。 這是解決方案https 的鏈接://www.kindacode.com/article/node-js-how-to-use-import-and-require-in-the-same-file/
import { createRequire } from "module";const require = createRequire(import.meta.url);
import normalizeUrl from 'normalize-url';
const express = require('express');
const router = express.Router();
const config = require('config');
const axios = require('axios');
我還在 package.json 中加入了"type": "module",
。 錯誤消息:` [0] const express = require('express');
[0] ReferenceError: require is not defined in ES module scope, you can use import instead [0] 此文件被視為 ES 模塊,因為它具有“.js”文件擴展名
您應該只使用從所有這些中導入。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.