![](/img/trans.png)
[英]Equivalent of a “require” statement in ES6: *.plugin(require('*'))
[英]What is the NodeJS ES6 equivalent of let PouchDB = require('pouchdb')?
我正在嘗試按照https://medium.com/beginners-guide-to-mobile-web-development/getting-started-with-pouchdb-f0f3d7baebab上的教程在服務器端 NodeJS 腳本中使用 PouchDB。
我陷入了第 1 步:
const PouchDB = require('pouchdb');
這導致:
ReferenceError: require is not defined in ES module scope,可以改用import
當我用谷歌搜索錯誤時,我被告知將"type":"module"
更改為"type":"commonjs"
。
不幸的是,我不能那樣做,因為它破壞了我已經在使用 ES6 import
的腳本的另一部分。
有沒有辦法重寫這個require
語句,讓它在 NodeJS ES6 中工作?
這是我迄今為止嘗試過的一些方法
我發現了大量相關問題,但似乎沒有一個專門針對pouchdb
模塊,不幸的是,我對 javascript 的了解還不夠深入,無法從找到的通用建議中概括和推斷出我需要的非常具體的修復。
例如,這個問題: ES6 equivalent of require('module').function() advises using import { foobar as Foobar } from 'module';
但是當我把它翻譯成import { PouchDB } from 'pouchdb';
然后我得到這個:
語法錯誤:未找到命名導出“PouchDB”。 請求的模塊“pouchdb”是一個 CommonJS 模塊,它可能不支持所有 module.exports 作為命名導出。 CommonJS 模塊始終可以通過默認導出導入,例如使用:
從“pouchdb”導入 pkg; const { PouchDB } = pkg;
當我嘗試按照上述錯誤消息中的建議使用import pkg from 'pouchdb'; const { PouchDB } = pkg;
import pkg from 'pouchdb'; const { PouchDB } = pkg;
,然后我得到這個:
類型錯誤:PouchDB 不是構造函數
TL;DR我需要知道如何在無法恢復到 CommonJS 時在 ES6 中實現一個簡單的require()
語句,因為同一腳本中的其他內容已經在使用 ES6 導入。
更新
這是import * as pDB from 'pouchdb'; console.log(pDB, typeof pDB);
import * as pDB from 'pouchdb'; console.log(pDB, typeof pDB);
:
[Module: null prototype] {
default: [Function: PouchDB] {
adapters: { leveldb: [Function], http: [Function], https: [Function] },
preferredAdapters: [ 'leveldb' ],
prefix: '_pouch_',
setMaxListeners: [Function: bound setMaxListeners],
getMaxListeners: [Function: bound getMaxListeners],
emit: [Function: bound emit],
addListener: [Function: bound addListener],
on: [Function: bound addListener],
prependListener: [Function: bound prependListener],
once: [Function: bound once],
prependOnceListener: [Function: bound prependOnceListener],
removeListener: [Function: bound removeListener],
off: [Function: bound removeListener],
removeAllListeners: [Function: bound removeAllListeners],
listeners: [Function: bound listeners],
rawListeners: [Function: bound rawListeners],
listenerCount: [Function: bound listenerCount],
eventNames: [Function: bound eventNames],
_destructionListeners: Map(0) {},
adapter: [Function (anonymous)],
plugin: [Function (anonymous)],
defaults: [Function (anonymous)],
fetch: [Function (anonymous)],
_changesFilterPlugin: {
validate: [Function: validate],
normalize: [Function: normalize],
shouldFilter: [Function: shouldFilter],
filter: [Function: filter]
},
version: '7.3.1',
replicate: [Function: replicateWrapper],
sync: [Function: sync]
}
} object
無需深入了解太多細節,舊的 CJS module.exports
和新的 ESM import/export
之間沒有簡單的映射(如果有工具可以自動處理它,您就不必問這個問題)。
ESM 在默認導出和命名導出之間做了非常明確的句法區分,在 CJS 方案中沒有嚴格的等價物。
import PouchDB from 'pouchdb';
將為您提供模塊的默認導出,這就是您想要的。 在排除這些類型的問題時,您始終可以進行星號導入:
import * as Whatever from 'some-package';
這會給你一個偽 object,它的屬性是 package 的所有導出,然后你可以反省以找到你想要的東西。 請注意,您不想在生產中為客戶端代碼執行星號導入,因為它會破壞大量未使用的代碼消除分析以減少包大小,但它對於開發中的故障排除很有用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.