简体   繁体   中英

Cannot find meteor module

I am trying to run a meteor app but keep getting the Cannot find module error when I try to import one of my files.

My folder directory:

/app/client/imports/api/tasks.js

/app/server/main.js

In my main.js I am trying to import tasks.js :

import '../client/imports/api/tasks.js';

This throws the error Error: Cannot find module '../client/imports/api/tasks.js' .

My tasks.js :

import { Mongo } from 'meteor/mongo';

export const Tasks = new Mongo.collection('tasks');

Does anyone know what might be going on?

You can't import a /client based file from the /server side. Files stored under a client directory are only bundled and made available to the client side of the application (the part that runs in a users browser). Files stored under a server directory are only bundled and made available on the server side, running via Node.

Get rid of the /client prefix from your tasks.js reference, to be able to reference the import from both sides. For example:

/app/imports/api/tasks.js

import { Mongo } from 'meteor/mongo';
const Tasks = new Mongo.Collection('tasks');
export default Tasks;

Then in /app/client/main.js or /app/server/main.js , you could reference the tasks.js file like:

import Tasks from '/imports/api/tasks.js';

The problem is in file structure. Your imports/ folder should be on the same level with client/ and server/ .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM