NodeJS 不会导入非 NodeJS 文件



我的网站上有以下两个文件:

  • firebase.js
  • gridsome-server.js

Firebase.js是一个"常规"javascript文件,包含以下内容:

import firebase from 'firebase/app'
import 'firebase/firestore'
const config = {
apiKey: '[APIKEY],
authDomain: '[AUTHDOMAIN],
databaseURL: '[DATABASEURL],
projectId: '[PROJECTID],
storageBucket: '[STORAGEBUCKET],
messagingSenderId: '[MESSAGINGSENDERID],
appId: '[APPID]'
}
firebase.initializeApp(config)
const db = firebase.firestore()
export default db

现在,我想将默认导出(db(导入gridsome-server.js- 它(我相信(是一个nodejs文件。 现在,我想我可以执行以下操作:const db = require('./src/config/firebase.js')- 但这不起作用。 我收到以下错误消息:

Initializing plugins...
SyntaxError: gridsome.server.js: Unexpected identifier (1:12)
> 1 | const db = require('./src/config/firebase')
|            ^
2 |
3 | module.exports = function(api) {
4 |   api.loadSource(({ addCollection }) => {
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

所以我require改为import- 如下所示:import db from './src/config/firebase. 但这也给了我一个错误:

Initializing plugins...
C:codetutorialsvuegridsomevuetify-gridsomegridsome.server.js:2
import db from './src/config/firebase'
^^
SyntaxError: Unexpected identifier
at Module._compile (internal/modules/cjs/loader.js:723:23)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Module.require (internal/modules/cjs/loader.js:692:17)
at require (internal/modules/cjs/helpers.js:25:18)
at Plugins.initialize (C:codetutorialsvuegridsomevuetify-gridsomenode_modulesgridsomelibappPlugins.js:45:11)
at App.init (C:codetutorialsvuegridsomevuetify-gridsomenode_modulesgridsomelibappApp.js:120:18)
at App.bootstrap (C:codetutorialsvuegridsomevuetify-gridsomenode_modulesgridsomelibappApp.js:49:16)
at module.exports (C:codetutorialsvuegridsomevuetify-gridsomenode_modulesgridsomelibappindex.js:12:18)
at module.exports (C:codetutorialsvuegridsomevuetify-gridsomenode_modulesgridsomelibdevelop.js:20:21)
at C:codetutorialsvuegridsomevuetify-gridsomenode_modulesgridsomeindex.js:44:12
at Command.program.command.description.option.option.action.args (C:codetutorialsvuegridsomevuetify-gridsomenode_modulesgridsomeindex.js:13:44)
at Command.listener (C:codetutorialsvuegridsomevuetify-gridsomenode_modulescommanderindex.js:315:8)
at Command.emit (events.js:198:13)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

所以我被困住了 - 如何将firebase.js导入gridsome-server.js

更新

我还尝试在firebase.js中更改为命名导出(即export const db(,并在gridsome-server.js中使用命名导入(即import { db } from...(。 这也会导致错误:

Initializing plugins...
C:codetutorialsvuegridsomevuetify-gridsomegridsome.server.js:2
import { db } from './src/config/firebase'
^
SyntaxError: Unexpected token {
at Module._compile (internal/modules/cjs/loader.js:723:23)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Module.require (internal/modules/cjs/loader.js:692:17)
at require (internal/modules/cjs/helpers.js:25:18)
at Plugins.initialize (C:codetutorialsvuegridsomevuetify-gridsomenode_modulesgridsomelibappPlugins.js:45:11)
at App.init (C:codetutorialsvuegridsomevuetify-gridsomenode_modulesgridsomelibappApp.js:120:18)
at App.bootstrap (C:codetutorialsvuegridsomevuetify-gridsomenode_modulesgridsomelibappApp.js:49:16)
at module.exports (C:codetutorialsvuegridsomevuetify-gridsomenode_modulesgridsomelibappindex.js:12:18)
at module.exports (C:codetutorialsvuegridsomevuetify-gridsomenode_modulesgridsomelibdevelop.js:20:21)
at C:codetutorialsvuegridsomevuetify-gridsomenode_modulesgridsomeindex.js:44:12
at Command.program.command.description.option.option.action.args (C:codetutorialsvuegridsomevuetify-gridsomenode_modulesgridsomeindex.js:13:44)
at Command.listener (C:codetutorialsvuegridsomevuetify-gridsomenode_modulescommanderindex.js:315:8)
at Command.emit (events.js:198:13)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

我相信问题在于您如何导入FireBase

import firebase from 'firebase/app'

摘自NPM网站上的文档

如果你使用带有 --experimental-modules 标志的原生 ES6 模块,你应该这样做:import firebase from 'firebase/app';

否则你应该像这样导入

const firebase = require('firebase');

相关内容

  • 没有找到相关文章

最新更新