gpt4 book ai didi

javascript - NodeJS 不会导入非 NodeJS 文件

转载 作者:行者123 更新时间:2023-12-02 22:23:35 30 4
gpt4 key购买 nike

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

  • 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:\code\tutorials\vue\gridsome\vuetify-gridsome\gridsome.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:\code\tutorials\vue\gridsome\vuetify-gridsome\node_modules\gridsome\lib\app\Plugins.js:45:11)
at App.init (C:\code\tutorials\vue\gridsome\vuetify-gridsome\node_modules\gridsome\lib\app\App.js:120:18)
at App.bootstrap (C:\code\tutorials\vue\gridsome\vuetify-gridsome\node_modules\gridsome\lib\app\App.js:49:16)
at module.exports (C:\code\tutorials\vue\gridsome\vuetify-gridsome\node_modules\gridsome\lib\app\index.js:12:18)
at module.exports (C:\code\tutorials\vue\gridsome\vuetify-gridsome\node_modules\gridsome\lib\develop.js:20:21)
at C:\code\tutorials\vue\gridsome\vuetify-gridsome\node_modules\gridsome\index.js:44:12
at Command.program.command.description.option.option.action.args (C:\code\tutorials\vue\gridsome\vuetify-gridsome\node_modules\gridsome\index.js:13:44)
at Command.listener (C:\code\tutorials\vue\gridsome\vuetify-gridsome\node_modules\commander\index.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:\code\tutorials\vue\gridsome\vuetify-gridsome\gridsome.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:\code\tutorials\vue\gridsome\vuetify-gridsome\node_modules\gridsome\lib\app\Plugins.js:45:11)
at App.init (C:\code\tutorials\vue\gridsome\vuetify-gridsome\node_modules\gridsome\lib\app\App.js:120:18)
at App.bootstrap (C:\code\tutorials\vue\gridsome\vuetify-gridsome\node_modules\gridsome\lib\app\App.js:49:16)
at module.exports (C:\code\tutorials\vue\gridsome\vuetify-gridsome\node_modules\gridsome\lib\app\index.js:12:18)
at module.exports (C:\code\tutorials\vue\gridsome\vuetify-gridsome\node_modules\gridsome\lib\develop.js:20:21)
at C:\code\tutorials\vue\gridsome\vuetify-gridsome\node_modules\gridsome\index.js:44:12
at Command.program.command.description.option.option.action.args (C:\code\tutorials\vue\gridsome\vuetify-gridsome\node_modules\gridsome\index.js:13:44)
at Command.listener (C:\code\tutorials\vue\gridsome\vuetify-gridsome\node_modules\commander\index.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 website 的文档

If you are using native ES6 module with --experimental-modules flag, you should do: import firebase from 'firebase/app';

否则你应该像这样导入

const firebase = require('firebase');

关于javascript - NodeJS 不会导入非 NodeJS 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59141024/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com